// simple derived class
class RatedPlayer: public TableTennisPlayer
private:
unsigned int rating:
public:
RatedPlayer (unsigned int r = 0, const char * fn = "none",
const char * in = "none", bool ht - false):
RatedPlayer (unsigned int r, const TableTennisPlayer & tp):
unsigned int Rating() ( return rating: ]
void ResetRating (unsigned int r) {rating = r; }
#endif
程序清单13.5是这两个类的方法定义。同样,也可以使用不同的文件,但将定义放在一起更为简便。
程序清单 13.5 tabtenn1.cpp
// tabtenn1.cpp -- base class methods and derived - class methods
#include "tabtennl.h"
#include <iostream>
Vinclude <cstring>
// TableTennisPlayer methods
TableTennisPlayer: :TableTennisPlayer (const char * fn, const char * ln.
bool ht)
std :: strncpy (firstname, fn, LIM - 1);
firstname [LIM - 1] = ' ':
std :: strncpy (lastname, ln, LIM - 1);
lastname [LIM - 1] = ' ':
hasTable - ht:
-
void TableTennisPlayer :: Name () const
std :: cout << lastname << ", " << firstname:
-
// RatedPlayer methods
RatedPlayer :: RatedPlayer (unsigned int r, const char * fn,
const char * In. bool ht): TableTennisPlayer (fn, ln, ht)
rating = r;
--
RatedPlayer :: RatedPlayer (unsigned int r, const TableTennisPlayer & tp)
: TableTennisPlayer (tp). rating (r)
程序清单 13.6 创建了 Table TennisPlayer 类和 RatedPlayer类的对象。请注意这两个类对象是如何使用
Table TennisPlayer 类的Name()和HasTable()方法的。
程序清单 13.6 usett1.cpp
// usettl.cpp -- using base class and derived class
#include <iostream>
#include "tabtennl.h"