CS 211:
Introduction to Computer Programming II
Instructor: Brian M. Dennis
Teaching Assistants:
Tom Lechner, Bin Lin, Rachel Goldsborough
http://www.cs.northwestern.edu/~bmd/cs211/

Issues with structs
Initialization
Not guaranteed
Done by hand
Packaging datatypes & behavior
Connecting
Reuse / extension
Salaried players

Basics of classes
// An example of declaring
// a new user defined type, using the class mech
class MLBPlayer {
public:
MLBPlayer(char* name, char* team);
private:
enum Hit { SINGLE = 0, DOUBLE, TRIPLE, HOMER };
char* name;
char* team;
int at_bats;
int walks;
int hits[4];
float batting_average;
bool bats_lefty;
};

Basics of classes
#include "MLBPlayer.h"
#include <cstring>
MLBPlayer::MLBPlayer(char* n, char* t)
{
int cnt = strlen(n) + 1;
name = new char[cnt];
strcpy(name, n);
cnt = strlen(t) + 1;
strcpy(team, t);
at_bats = walks = 0;
bats_lefty = false;
batting_average = 0.0;
for (int i = 0; I < 4; i++) { hits[i] = 0; };
}

Basics of classes
// Moving some private fields to public for convenience
class MLBPlayer {
public:
MLBPlayer(char* name, char* team);
int at_bats;
int walks;
float batting_average;
private:
enum Hit { SINGLE = 0, DOUBLE, TRIPLE, HOMER };
char* name;
char* team;
int hits[4];
bool bats_lefty;
};

Basics of classes
#include <iostream>
using namespace std;
#include "MLBPlayer.h"
// Accessing public member variables
int main(int argc, char* argv[]) {
MLBPlayer sammy_sosa("Sammy Sosa", "Chicago Cubs");
MLBPlayer* bmd_wishes = &sammy_sosa;
sammy_sosa.at_bats = 1;
int total_hits = 0;
for (int i = 0; i < 4; i++) {
total_hits = sammy_sosa.hits[i];
};
bmd_wishes->batting_average =
total_hits / bmd_wishes->at_bats;
return 0;
}

Basics of classes
// Instead of public fields, let's write public accessor funcs
class MLBPlayer {
public:
MLBPlayer(char* name, char* team);
enum Hit { SINGLE = 0, DOUBLE, TRIPLE, HOMER };
int totalHits();
float battingAverage();
void addHit(int hit);
void strikeout();
private:
char* name; char* team;
int at_bats; int walks;
int hits[4];
float batting_average;
bool bats_lefty;
};

Basics of classes
int MLBPlayer::totalHits() {
int hits_count = 0;
for (int i = 0; i < 4; i++) {
hits_count += hits[i];
}
return hits_count;
}
void MLBPlayer::addHit(int hit) {
at_bats++;
if ((SINGLE <= hit) && (hit <= HOMER)) {
hits[hit] += 1;
}
}

Basics of classes
float MLBPlayer::battingAverage() {
return totalHits() / (at_bats * 1.0f);
}
void MLBPlayer::strikeout() {
at_bats++;
}

Basics of classes
#include <iostream>
using namespace std;
#include "MLBPlayer.h"
// Now using public accessor functions
int main(int argc, char* argv[]) {
MLBPlayer sammy_sosa("Sammy Sosa", "Chicago Cubs");
MLBPlayer* bmd_wishes = &sammy_sosa;
sammy_sosa.addHit(MLBPlayer::SINGLE);
sammy_sosa.strikeout();
sammy_sosa.strikeout();
float ba = sammy_sosa.battingAverage();
return 0;
}

Basics of classes
#include <iostream>
using namespace std;
#include "MLBPlayer.h"
// Now using public accessor functions
int main(int argc, char* argv[]) {
MLBPlayer sammy_sosa("Sammy Sosa", "Chicago Cubs");
MLBPlayer* bmd_wishes = &sammy_sosa;
bmd_wishes->addHit(MLBPlayer::SINGLE);
bmd_wishes->strikeout();
bmd_wishes->strikeout();
float ba = bmd_wishes->battingAverage();
return 0;
}

Basics of classes
// Use overloading to define default constructor
class MLBPlayer {
public:
MLBPlayer(char* name, char* team);
MLBPlayer();
enum Hit { SINGLE = 0, DOUBLE, TRIPLE, HOMER }; int totalHits();
float battingAverage();
void addHit(int hit);
void strikeout();
private:
char* name; char* team;
int at_bats; int walks;
int hits[4];
float batting_average;
bool bats_lefty;
};

Basics of classes
#include "MLBPlayer.h"
// Stuff elided
MLBPlayer::MLBPlayer() {
char* n = "Joe Schlabotnik";
char* t = "Waffletown Syrups";
int cnt = strlen(n) + 1;
name = new char[cnt];
strcpy(name, n);
cnt = strlen(t) + 1;
strcpy(team, t);
at_bats = walks = 0;
bats_lefty = false;
batting_average = 0.0;
for (int i = 0; i < 4; i++) { hits[i] = 0 };
}

Basics of classes
#include <iostream>
using namespace std;
#include "MLBPlayer.h"
int main(int argc, char* argv[]) {
// Imagine all our old stuff here.
MLBPlayer black_sox[9];
MLBPlayer* joe_s = new MLBPlayer;
joe_s = new MLBPlayer();
joe_s = new MLBPlayer("Joe Schlabotnik", "Waffletown Syrups");
return 0;
}

Dynamic allocation
#include <cstring>
// Leaves us with a dangling pointer, also wastes space
char* dup_string(char* s) {
const int max_buf = 1024;
char buf[max_buf];
strcpy(buf, s);
return buf;
}
// Using dynamic allocation we're okay
char* dup_string(char* s) {
int cnt = strlen(s) + 1;
char* new_string = new char[cnt];
return new_string;
}

That’s a Wrap
Take away
Classes are an improved form of structs
Know how to declare classes, define member functions
Dynamic allocation
Space managed by program at run time
Binary representations