1 / 14

Inheritance

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 Center decided to keep track of those members who play table tennis. Consider the base class Player listed on the next slide.

Sophia
Download Presentation

Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance ITK 169 Fall 2003

  2. 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.

  3. 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); };

  4. 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.

  5. 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; } };

  6. 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.)

  7. So… • An object of type RatedPlayer has in store: • 3 data members of Player • fName, lName; hasTable; • plus a rating. • An object of type RatedPlayercan 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.

  8. 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.

  9. 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.

  10. 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.

  11. 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.

  12. 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";

  13. 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)?

More Related