|
1
|
- Instructor: Brian M. Dennis
- Teaching Assistants:
- Tom Lechner, Bin Lin, Rachel Goldsborough
- http://www.cs.northwestern.edu/~bmd/cs211/
|
|
2
|
- Data abstraction feature
- User defined datatype
- Collection
- Heterogeneous
- Typed fields
- Indexed by name
- Access control
- Controlled initialization + deallocation
- Encapsulation
- data + operations on that data
|
|
3
|
- // Use overloading to define default constructor
- class MLBPlayer {
- public: // Access specifier
- MLBPlayer(char* name, char* team); // Constructor
- MLBPlayer(); // Default
constructor
- // Member functions
- int totalHits();
- float battingAverage();
- void addHit(int hit);
- void strikeout();
- enum Hit { SINGLE = 0, DOUBLE, TRIPLE, HOMER };
- private:
- // Member variables
- char* name; char* team;
- int at_bats; int walks;
- int hits[4];
- float batting_average; bool bats_lefty;
- };
|
|
4
|
- #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;
- team = new char[cnt]; // fixed
up from last lecture
- strcpy(team, t);
- at_bats = walks = 0;
- bats_lefty = false;
- batting_average = 0.0;
- for (int i = 0; I < 4; i++) { hits[i] = 0; };
- }
|
|
5
|
- 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;
- }
- }
|
|
6
|
- #include "MLBPlayer.h"
- #include <cstring>
- int main(int argc, char* argv[]) {
- MLBPlayer cubs_starters[9];
- cubs_starters[0] = MLBPlayer("Corey Patterson",
- "Chicago Cubs");
- …
- cubs_starters[3] = MLBPlayer("Sammy Sosa",
- "Chicago Cubs");
- …
- pinch_hit(8, "Tom Goodwin");
- }
|
|
7
|
- // stuff from above
- bool pinch_hit(int pos, char* name,
- MLBPlayer* lineup) {
- MLBPlayer on_deck(name,
lineup[pos].getTeam());
- if (!calledBack(name)) {
- lineup[pos] = on_deck;
- return false;
- } else {
- return true;
- }
- // What happens to on_deck here?
- }
|
|
8
|
- class MLBPlayer {
- public:
- MLBPlayer(char* name, char* team);
- MLBPlayer();
- ~MLBPlayer(); // Destructor for the class
- 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;
- };
|
|
9
|
- // Defining the (and only) constructor
- // for a class
- MLBPlayer::~MLBPlayer() {
- delete team; // Give back space for team
- delete name; // Give back space for name
- }
- // Except now we have another problem!!
|
|
10
|
- // stuff from above
- bool pinch_hit(int pos, char* name,
- MLBPlayer* lineup) {
- MLBPlayer on_deck(name,
lineup[pos].getTeam());
- if (!calledBack(name)) {
- lineup[pos] = on_deck; // But we member copied!
- return false;
- } else {
- return true;
- }
- // On deck gets cleaned up!
- }
|
|
11
|
- #include "MLBPlayer.h"
- #include <cstring>
- int main(int argc, char* argv[]) {
- MLBPlayer cubs_starters[9];
- MLBPlayer* leadoff_hitter = &cubs_starters[0];
- MLBPlayer* closer = new MLBPlayer("Eric Gagne",
- "LA Dodgers");
- MLBPlayers* dodgers = new MLBPlayers[9];
- delete cubs_starters;
- delete [] cubs_starters;
- delete leadoff_hitter;
- delete closer;
- delete dodgers;
- delete [] dodgers;
- }
|
|
12
|
- #include "MLBPlayer.h"
- MLBPlayer ichiro("Ichiro", "Seattle Mariners");
- int main(int argc, char* argv[]) {
- MLBPlayer cubs_starters[9];
- {
- MLBPlayer local_player;
- on_injured_reserve(local_player);
- MLBPlayer* dynamic_player = new MLBPlayer;
- }
- MLBPlayer big_unit("Randy Johnson",
"Diamondbacks");
- return 0;
- }
- void on_injured_reserve(MLBPlayer p) {
- static cnt = 0;
- static MLBPlayer reserve[4];
- reserve[cnt] = p;
- cnt++;
- }
|
|
13
|
|
|
14
|
- Takeaways
- Memberwise copy
- Instance allocation & deallocation
- Setters and getters
|