先上一个简单的代码
头文件,jicheng.h
class TableTennisPlayer
{
private:
enum {LIM = 20};
char firstname[LIM];
char lastname[LIM];
bool hasTable;
public:
TableTennisPlayer(const char * fn = "none",const char *ln = "none",bool ht = false);
void Name() const;
bool HasTable() const { return hasTable;};
void ResetTable (bool v) {hasTable = v;};
};
源文件:
#include "jicheng.h"
#include <iostream>
#include <cstring>
TableTennisPlayer::TableTennisPlayer(const char *fn, const char *ln,bool ht)
{
std::strncpy(firstname,fn,LIM - 1);
firstname[LIM - 1] = '\0';
std::strncpy(lastname,ln,LIM - 1);
lastname[LIM - 1] = '\0';
hasTable = ht;
}
void TableTennisPlayer::Name() const
{
std::cout << lastname << "." << firstname;
}
main文件
#include "jicheng.h"
#include <iostream>
int main()
{
using std::cout;
TableTennisPlayer player1 ("Ma","Lin",true);
TableTennisPlayer player2 ("Wang","Nan",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\n";
else
cout << ": hasn't a table\n";
}
如何继承这个类呢,比如我们要创建一个类,表示参加过锦标赛的乒乓球运动员,我们就可以继承上面的那个类,形如
class Ratedplayer:public TableTennisPlayer
{
private:
unsigned int rating;
public:
}
Ratedplayer类有TableTennisPlayer派生而来,:冒号指出Ratedplayer类的基类是TableTennisPlayer类。上述特殊的声明头表明TableTennisPlayer是一个公有基类,这被称为公有派生,派生类对象包含基类对象。使用公有派生,基类的公有成员将成为派生类的公有成员,基类的私有部分也将成为派生类的一部分,但是只能通过积累的共有和保护方法来访问。
派生类还应继续添加自己的构造函数,额外的数据成员和成员函数。
我们来更新一下继承类,定义私有数据,并加上自身的构造函数和其他的方法
class Ratedplayer:public TableTennisPlayer
{
private:
unsigned int rating;
public:
Ratedplayer (unsigned int r = 0, const char * fn = "none",
const char * ln = "none",bool ht = false);
Ratedplayer (unsigned int r, const TableTennisPlayer & tp);
unsigned int Rating() {return rating;}
void ResetRating (unsigned int r ) {rating = r;}
};
派生类不能直接访问基类的私有成员,而必须通过基类方法进行访问。例如,RatedPlayer的构造函数不能直接设置继承的成员,而必须使用基类的公有方法来访问私有的基类成员。具体来说,派生类构造函数必须使用基类构造函数。
创建派生类对象时,程序首先创建基类对象。从概念上说,这意味着基类对象应当在程序进入派生类构造函数之前被创建。C++使用成员初始化列表句法来完成这种工作。例如。下面时第一个RatedPlayer构造函数的代码。
Ratedplayer::Ratedplayer (unsigned int r,const char *fn,
const char * ln,bool ht):TableTennisPlayer (fn,ln,ht)
{
rating = r;
}
其中TableTennisPlayer(fn,ln,ht)是成员初始化列表,它是可执行的代码,它调用TableTennisPlayer构造函数。假如程序包含以下声明:
RatedPlayer rplayer1(1140,"Marry","Duck",ture);
则RatedPlayer构造函数将把实参"Marry","Duck"和true赋给形参fn,ln,和ht,然后将这些参数作为实参传递给TableTennisPlayer构造函数,后者将创建一个嵌套TableTennisPlayer对象,并将参数存储在对象中,然后,程序进入RatedPlayer构造函数,完成RatedPlayer对象的创建,并将参数r的值赋给rating成员。
如果省略成员初始化列表,程序将使用的默认的基类构造函数。
使用派生类:
要使用派生类,程序必须要能够访问基类声明。我们将两个类的声明置于同一个头文件中
class TableTennisPlayer
{
private:
enum {LIM = 20};
char firstname[LIM];
char lastname[LIM];
bool hasTable;
public:
TableTennisPlayer(const char * fn = "none",const char *ln = "none",bool ht = false);
void Name() const;
bool HasTable() const { return hasTable;};
void ResetTable (bool v) {hasTable = v;};
};
class Ratedplayer:public TableTennisPlayer
{
private:
unsigned int rating;
public:
Ratedplayer (unsigned int r = 0, const char * fn = "none",
const char * ln = "none",bool ht = false);
unsigned int Rating() {return rating;}
void ResetRating (unsigned int r ) {rating = r;}
};class TableTennisPlayer
{
private:
enum {LIM = 20};
char firstname[LIM];
char lastname[LIM];
bool hasTable;
public:
TableTennisPlayer(const char * fn = "none",const char *ln = "none",bool ht = false);
void Name() const;
bool HasTable() const { return hasTable;};
void ResetTable (bool v) {hasTable = v;};
};
class Ratedplayer:public TableTennisPlayer
{
private:
unsigned int rating;
public:
Ratedplayer (unsigned int r = 0, const char * fn = "none",
const char * ln = "none",bool ht = false);
unsigned int Rating() {return rating;}
void ResetRating (unsigned int r ) {rating = r;}
};
方法的实现
#include <iostream>
#include <cstring>
#include "jicheng.h" #include <iostream>
#include <cstring>
TableTennisPlayer::TableTennisPlayer(const char *fn, const char *ln,bool ht)
{
std::strncpy(firstname,fn,LIM - 1);
firstname[LIM - 1] = '\0';
std::strncpy(lastname,ln,LIM - 1);
lastname[LIM - 1] = '\0';
hasTable = ht;
}
void TableTennisPlayer::Name() const
{
std::cout << lastname << "." << firstname;
}
Ratedplayer::Ratedplayer (unsigned int r,const char *fn,
const char * ln,bool ht):TableTennisPlayer (fn,ln,ht)
{
rating = r;
}
main.c文件
#include "jicheng.h"
#include <iostream>
int main()
{
using std::cout;
using std::endl;
TableTennisPlayer player1 ("Ma","Lin",true);
Ratedplayer rplayer1(1140,"wang","nan",true);
rplayer1.Name();
if (rplayer1.HasTable())
cout << ": has a table\n";
else
cout << ": hasn't a table\n";
player1.Name();
if (player1.HasTable())
cout << ": has a table\n";
else
cout << ": hasn't a table\n";
rplayer1.Name();
cout << ": rating: " << rplayer1.Rating() << endl;
return 0;
}