jlz

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 13 Stories :: 0 Comments :: 0 Trackbacks

常用链接

留言簿

我参与的团队

搜索

  •  

最新评论

#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;
 
}

posted on 2008-09-21 16:04 jz 阅读(71) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理