jlz

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

常用链接

留言簿

我参与的团队

搜索

  •  

最新评论

 

#include<iostream>
#include<cstring>

#ifndef TABTENNO_H_
#define TABTENNO_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;
 }
};
#endif


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;

}

int main(void)
{
 using std::cout;
 TableTennisPlayer player1("chuck","Blizzard",true);
 TableTennisPlayer player2("Tarza","Boomdea",false);

 player1.Name();
 if(player1.HasTable())   //不可直接访问private 成员
 {
  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";

 return 0;

}


 

posted on 2008-09-21 15:04 jz 阅读(60) 评论(0)  编辑 收藏 引用 所属分类: c++ primer plus 读书笔记