Deriving a Class

Some members of the Webtown Social Club have played in local table tennis tournaments, and they demand a class that includes the point ratings they’ve earned through their play. Rather than start from scratch, you can derive a class from the TableTennisPlayer class. The first step is to have the RatedPlayer class declaration show that it derives from the TableTennisPlayer class:

// RatedPlayer derives from the TableTennisPlayer base class
class RatedPlayer : public TableTennisPlayer
{
...
};

The colon indicates that the RatedPlayer class is based on the TableTennisPlayer class. This particular heading indicates that TableTennisPlayer is a public base class; this is termed public derivation. An object of a derived class incorporates a base class object. With public derivation, the public members of the base class become public members of the derived class. The private portions of a base class become part of the derived class, but they can be accessed only through public and protected methods of the base class. (We’ll get to protected members in a bit.)

What does this accomplish? If you declare a RatedPlayer object, it has the following special properties:

• An object of the derived type has stored within it the data members of the base type. (The derived class inherits the base-class implementation.)

• An object of the derived type can use the methods of the base type. (The derived class inherits the base-class interface.)

Thus, a RatedPlayer object can store the first name and last name of each player and whether the player has a table. Also a RatedPlayer object can use the Name(), HasTable(), and ResetTable() methods from the TableTennisPlayer class (see Figure 13.1 for another example).

Figure 13.1. Base-class and derived-class objects.

Image

What needs to be added to these inherited features?

• A derived class needs its own constructors.

• A derived class can add additional data members and member functions as needed.

In this particular case, the class needs one more data member to hold the ratings value. It should also have a method for retrieving the rating and a method for resetting the rating. So the class declaration could look like this:

// simple derived class
class RatedPlayer : public TableTennisPlayer
{
private:
    unsigned int rating;     // add a data member
public:
    RatedPlayer (unsigned int r = 0, const string & fn = "none",
                 const string & ln = "none", bool ht = false);
    RatedPlayer(unsigned int r, const TableTennisPlayer & tp);
    unsigned int Rating() const { return rating; }  // add a method
    void ResetRating (unsigned int r) {rating = r;} // add a method
};

The constructors have to provide data for the new members, if any, and for the inherited members. The first RatedPlayer constructor uses a separate formal parameter for each member, and the second RatedPlayer constructor uses a TableTennisPlayer parameter, which bundles three items (firstname, lastname, and hasTable) into a single unit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.97.216