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/ | |
| Data abstraction feature | ||
| User defined datatype | ||
| Collection | ||
| Compound data | ||
| Heterogeneous | ||
| Typed fields | ||
| Indexed by name | ||
| Access control | ||
| Information hiding | ||
| Controlled initialization + deallocation | ||
| Encapsulation | ||
| data + operations on that data | ||
| // 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; | |
| }; |
| #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; }; | |
| } | |
| 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; | |
| } | |
| } | |
| #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"); | |
| } | |
| // 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? | |
| } |
| 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; | |
| }; |
| // 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!! |
| // 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! | |
| } |
| #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; | |
| } |
| #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++; | |
| } |
| Time permitting |
| Takeaways | ||
| Memberwise copy | ||
| Instance allocation & deallocation | ||
| Setters and getters | ||