一个普通的类继承,包括构造函数传对象引用和参数列表初始化
#ifndef TABLETENN1_H_
#define TABLETENN1_H_
#include<string>
using namespace std;
class TableTennisPlayer
{
private:
string firstname;
string lastname;
bool hasTable;
public:
TableTennisPlayer(const string &fn="none",const string &ln="none",bool ht=false);
void Name()const;
bool HasTable()const
{
return hasTable;
}
void ResetTable(bool v)
{
hasTable=v;
}
};
class RatedPlayer:public TableTennisPlayer
{
private:
int rating;
public:
RatedPlayer(int r=0,const string &fn="none",const string &ln="none",bool ht=false);
RatedPlayer(int r,const TableTennisPlayer &tp);
int Rating()const
{ return rating;}
void ResetRating(int r)
{ rating=r;}
};
#endif
#include<iostream>
#include"tabtenn1.h"
using namespace std;
TableTennisPlayer::TableTennisPlayer(const string &fn,const string &ln,bool ht)
{
firstname=fn;
lastname=ln;
hasTable=ht;
}
void TableTennisPlayer::Name()const
{
cout<<lastname<<", "<<firstname;
}
RatedPlayer::RatedPlayer(int r,const string &fn,const string &ln,bool ht)
{
TableTennisPlayer(fn,ln,ht);
rating=r;
}
RatedPlayer::RatedPlayer(int r, const TableTennisPlayer &tp):TableTennisPlayer(tp)
{
rating=r;
}
#include<iostream>
#include"tabtenn1.h"
using namespace std;
int main()
{
TableTennisPlayer player1("Tara","Boomdea",false);
RatedPlayer rplayer1(1140,"Maloory","Duck",true);
rplayer1.Name();
if(rplayer1.HasTable())
cout<<" has a table!"<<endl;
else
cout<<" hasn't a table!"<<endl;
player1.Name();
if(player1.HasTable())
cout<<" has a table!"<<endl;
else
cout<<" hasn't a table!"<<endl;
cout<<"Name:";
rplayer1.Name();
cout<<";Rating: "<<rplayer1.Rating();
RatedPlayer rplayer2(1212,player1);
cout<<"Name: ";
rplayer2.Name();
cout<<";Rating: "<<rplayer2.Rating()<<endl;
return 0;
}