您的位置:首页 > 编程语言 > C语言/C++

C++ Primer Plus学习:第十三章 类继承(1)

2011-09-23 09:09 417 查看
基础类

eg

tabtenn0.h

#ifndef TABTENN0_H_
#define TABTENN0_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


tabtenn0.cpp

#include "tabtenn0.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;
}


useten.cpp

#include <iostream>
#include "tabtenn0.h"

int main()
{
using std::cout;
//true=HasTable()
TableTennisPlayer player1("Chuck","Blizzard",true);
//false=!HasTable()
TableTennisPlayer player2("Tara","Booddea",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";

system("pause");
return 0;
}


派生类

构造函数要点

基类对象首先被创建

派生类构造函数应通过成员初始化列表将基类信息传递给基类构造函数

派生类构造函数应初始化派生类新增的数据成员

eg

tabtenn1.h

#ifndef TABTENN1_H_
#define TABTENN1_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;};
};

//simple derived class
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;}
};

#endif


tabtenn1.cpp

#include "tabtenn1.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 methods
//TableTennisTable(fn,ln,ht)从基类中全传值
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)
{

}


usett2.cpp

#include <iostream>
#include "tabtenn1.h"

int main()
{
using std::cout;
using std::endl;
//true=HasTable()
TableTennisPlayer player1("Tara","Booddea",false);
RatedPlayer rplayer1(1140,"Mallory","Duck",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";

cout<<"Name:";
rplayer1.Name();
cout<<":Rating: "<<rplayer1.Rating()<<endl;
RatedPlayer rplayer2(1212,player1);
cout<<"Name:";
rplayer2.Name();
cout<<":Rating: "<<rplayer2.Rating()<<endl;

system("pause");
return 0;
}


关系

派生类对象可以使用基类的方法,条件是方法不是私有的

不可以将基类对象和地址赋给派生类引用和指针
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: