const string & ln = "none", bool ht = false);
void Name() const;
bool HasTable() const { return hasTable; };
void ResetTable(bool v) { hasTable = v; };
};
#endif
Listing 13.2. tabtenn0.cpp
//tabtenn0.cpp -- simple base-class methods
#include "tabtenn0.h"
#include
TableTennisPlayer::TableTennisPlayer (const string & fn,
const string & ln, bool ht) : firstname(fn),
lastname(ln), hasTable(ht) {}
void TableTennisPlayer::Name() const
{
std::cout << lastname << ", " << firstname;
}
All the TableTennisPlayer class does is keep track of the players’ names and whether they have tables. There are a couple of points to notice. First, the class uses the standard string class to hold the names. This is more convenient, flexible, and safer than using a character array. And it is rather more professional than the String class of Chapter 12, “Classes and Dynamic Memory Allocation.” Second, the constructor uses the member initializer list syntax introduced in Chapter 12. You could also do this:
TableTennisPlayer::TableTennisPlayer (const string & fn,
const string & ln, bool ht)
{
firstname = fn;
lastname = ln;
hasTable = ht;
}
However, this approach has the effect of first calling the default string constructor for firstname and then invoking the string assignment operator to reset firstname to fn. But the member initializer list syntax saves a step by just using the string copy constructor to initialize firstname to fn.
Listing 13.3 shows this modest class in action.
Listing 13.3. usett0.cpp
// usett0.cpp -- using a base class
#include
#include "tabtenn0.h"
int main ( void )
{
using std::cout;
TableTennisPlayer player1("Chuck", "Blizzard", true);
TableTennisPlayer player2("Tara", "Boomdea", false);
player1.Name();
if (player1.HasTable())
cout << ": has a table.\n";
else
cout << ": hasn't a table.\n";
player2.Name();
if (player2.HasTable())
cout << ": has a table";
else
cout << ": hasn't a table.\n";
return 0;
}
And here’s the output of the program in Listings 13.1, 13.2, and 13.3:
Blizzard, Chuck: has a table.
Boomdea, Tara: hasn't a table.
Note that the program uses constructors with C-style string arguments:
TableTennisPlayer player1("Chuck", "Blizzard", true);
TableTennisPlayer player2("Tara", "Boomdea", false);
But the formal parameters for the constructor were declared as type const string &. This is a type mismatch, but the string class, much like the String class of Chapter 12, has a constructor with a const char * parameter, and that constructor is used automatically to create a string object initialized by the C-style string. In short, you can use either a string object or a C-style string as an argument to the TableTennisPlayer constructor. The first invokes a string constructor with a const string & parameter, and the second invokes a string constructor with a const char * parameter.
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
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.
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.