Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance ITK 169 Fall 2003 Base Class Also called the Parent Class This is the class that later classes will be build on. Suppose the Red Bird Rec.

Similar presentations


Presentation on theme: "Inheritance ITK 169 Fall 2003 Base Class Also called the Parent Class This is the class that later classes will be build on. Suppose the Red Bird Rec."— Presentation transcript:

1

2 Inheritance ITK 169 Fall 2003

3 Base Class Also called the Parent Class This is the class that later classes will be build on. Suppose the Red Bird Rec Center decided to keep track of those members who play table tennis. Consider the base class Player listed on the next slide.

4 class Player { private: string fName, lName; bool hasTable; public: // default and special constructor in one using default values… Player(const string& first = "empty", const string& last = "empty", bool table = false); void set_fName(const string& first) { fName = first; } void set_lName(const string& last) { lName = last; } void set_hasTable(bool table) { hasTable = table; } string get_fullnameLF()const { return (lName + ", " + fName); } string get_fullnameFL()const { return (fName + " " + lName); } bool get_hasTable()const { return hasTable; } void printNameFL(ostream& outs) { outs << fName << " " << lName; } friend ostream& operator <<(ostream& outs, const Player& aPlayer); };

5 Derived Class Some members have played in local table tennis tournaments. They would like the class to include the point ratings they’ve earned. Rather than start from scratch, you can derive a class from the Player class. RatedPlayer will be the derived class.

6 Notice - : public Player class RatedPlayer : public Player { private: unsigned int rating; public: // default and special constructor in one using default values… RatedPlayer(unsigned int rt = 0, const string& first = "empty", const string& last = "empty", bool table = false); // constructor to add a rating to an existing player RatedPlayer(unsigned int rate, const Player& aPlayer); void set_rating(unsigned int num) { rating = num; } unsigned int get_rating() { return rating; } };

7 Public Derivation In the class header : public Player is added to indicate that –RatedPlayer is derived from Player, –and that Player is a public base class. With public derivation, all public members of the base class become public members of the derived class. The private portions of the base class become part of the derived class but they can only be accessed through public and protected methods of the base class. (More on protected methods later.)

8 So… An object of type RatedPlayer has in store: – 3 data members of Player fName, lName; hasTable; – plus a rating. An object of type RatedPlayer can use the methods of the base type and its own class. An object of type Player has only 3 data members and can only access Player methods.

9 Derived Requirements A derived class can add additional data members and member functions as needed. A derived class needs its own constructors. –Note the constructors must provide data for the any new data member(s) AND the base data members.

10 Access Considerations A derived class does not have direct access to the private members of the base class. It has to work through the base class methods. In particular, the derived constructors have to use the base class constructors.

11 Derived Constructors Each constructor includes a call to a Player constructor at the end of the parameter list - note the use of a : RatedPlayer::RatedPlayer(unsigned int rate, const string& first, const string& last, bool table) : Player(first, last, table) { rating = rate; } RatedPlayer::RatedPlayer(unsigned int rate, const Player& aPlayer) : Player(aPlayer), rating(rate) { } This is call an initializer list. Note: We can have more than just the Player constructor call here.

12 Key Points to Remember The base class object is constructed first. The derived class constructor should pass base class information to a base class constructor via a member initialization list. The derived class constructor should initialize those data members that were added to the derived class.

13 Using our 2 Classes Player mickey("Mickey", "Mouse"); RatedPlayer daffy(10, "Daffy", "Duck"); cout << mickey << " plays occasionally for fun.\n"; cout << daffy << " is a serious tournament player.\n"; daffy.printNameFL(cout); cout << " is rated " << daffy.get_rating( ) << " out of 50, in the city tournaments.\n";

14 Private Members Why would a derived class NOT have direct access to private data members? What about private functions? Is there a “go around”? Is it a good idea to allow direct access to private members (data or functions)?


Download ppt "Inheritance ITK 169 Fall 2003 Base Class Also called the Parent Class This is the class that later classes will be build on. Suppose the Red Bird Rec."

Similar presentations


Ads by Google