#ifndef TABTENN1_H_
#define TABTENNI_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;};
};
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 ResetTable(unsigned int r){ rating = r;}
};
#endif
#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;
}
RatedPlayer::RatedPlayer(unsigned int r, const TableTennisPlayer &tp):TableTennisPlayer(tp),rating(r)
{
}
int main()
{
using std::cout;
using std::endl;
TableTennisPlayer player1("Tara","Boomdea",false);
RatedPlayer rplayer1(9999999,"Mallory","Duck",true);
rplayer1.Name();
if(rplayer1.HasTable())
cout<<": has a table.\n";
else
cout<<":has't a table.\n";
player1.Name();
if(player1.HasTable())
cout<<": has a table.\n";
else
cout<<":has't a table.\n";
cout <<"Name:";
rplayer1.Name();
cout<<":Rating:"<<rplayer1.Rating()<<endl;
RatedPlayer rplayer2(1212,player1); // 这点注意
cout<<"Name:";
rplayer2.Name();
cout<<":Rating:"<<rplayer2.Rating()<<endl;
return 0;
}