学习目标:
1、理解继承的使用和语法
2、掌握基类、派生类构造函数、析构函数编写方法和调用顺序
3、理解类型兼容原则和适用情形
4、了解多继承和虚继承
5、掌握多态的实现机制
6、理解和掌握纯虚函数和抽象类的使用
示例:
inheritcopy.cpp
#include <iostream>#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; }};void TableTennisPlayer::Name() const{cout<<firstname<<" . "<<lastname;}TableTennisPlayer::TableTennisPlayer(const string &fn,const string &ln,bool ht):firstname(fn),lastname(ln),hasTable(ht){}class RatedPlayer:public TableTennisPlayer{private:unsigned int rating;public:RatedPlayer(unsigned int r=0,const string &fn="none",const string &ln="none",bool ht=false);RatedPlayer(unsigned int r,const TableTennisPlayer &tp);unsigned int Rating() const { return rating;}void ResetRating(unsigned int r) {rating =r;}};RatedPlayer::RatedPlayer(unsigned int r, const string &fn,const string &ln,bool ht):TableTennisPlayer(fn,ln,ht){rating=r;}RatedPlayer::RatedPlayer(unsigned int r,const TableTennisPlayer &tp):TableTennisPlayer(tp),rating(r){//rating=r;}int main(){TableTennisPlayer player1("Malcony","Duck",false);RatedPlayer rplayer1(1140,"Tara","Sydney",true);rplayer1.Name() ;if(rplayer1.HasTable()){cout<<" has a table.\n";}elsecout<<"hasn't a table.\n";player1.Name() ;if(player1.HasTable())cout<<" has a table.\n";elsecout<<" hasn't a table.\n";rplayer1.Name() ;cout<<"; Rating: "<<rplayer1.Rating() <<endl;RatedPlayer rplayer2(1212,player1);cout<<"Name: ";rplayer2.Name() ;cout<<"; Rating: "<<rplayer2.Rating() <<endl;}
一、继承和派生
可以用一个已存在的类创建一个新类实现代码重用,并实现功能上的扩展,即类的继承(inherit)和派生(derive),只所以有这两个说法,是因为参照物不同,如由A类创建了B类,即A派生出了B,B继承了A,A是基类,B是派生类。
1、继承和派生的概念与特性
继承和派生即从已有类生成一个新类,实现代码重用。
总的说,如果符合以下两个特征就可以使用继承或派生:
A、两个类之间是is-a的关系,即一个类是另一个类的更具体化的表现,从抽象点到具体点,两个类有较多共同点。
B、从已有类继承行为和属性,并对其进行适当扩展和改写,增加新功能的同时实现代码重用。
2、回顾类之间的关系及类图法
依赖: 
泛化: 
聚合: 
拥有:
组合:
理解上述几个关系表示法。
举例:建筑物 工厂大楼 大学大楼 医院大楼 什么关系?
3、从程序实现角度分析继承与派生
A、派生类:
可以已有类的基础上添加功能
可以给类添加数据
可以修改类方法的行为或者说功能
B、派生类对象:
存储了基类的数据成员
可以使用基类的方法
C、需要在派生类中做什么?
定义派生类构造函数或拷贝构造函数
根据需要添加数据成员或成员函数
D、派生类构造函数的要点
首先创建基类对象
派生类构造函数应通过成员初始化列表将基类信息传递给基类构造函数
派生类构造函数应初始化派生类新增数据成员
二、派生类与基类的特殊关系
1、对于公有继承,派生类对象可以使用基类的公有方法。
RatedPlayer rplayer1(1140,“Malcony”,“Duck”,true);
Rplayer1.Name();
2、基类指针可以在不进行显示类型转换的情况下指向派生类对象;基类引用可以不进行显式类型转换的情况下引用派生类对象。
RatedPlayer rplayer1(1140,“Malcony”,“Duck”,true);
TableTennisPlayer &rt=rplayer;
TableTennisPlayer pt=&rplayer;
rt.Name();
pt->Name();
说明:
A、基类引用和基类指针只能调用基类方法,而不能调用派生类方法。
B1、基类引用派生类对象和基类指针指向派生类对象是单向的,即派生类对象不能做为基类的引用,派生类指针不能指向基类对象。
B2、基类引用派生类对象和基类指针指向派生类对象后只能调用基类函数而不能调用派生类函数。
TableTennisPlayer player(“Betsy”,“Bloop”,true);
RatedPlayer &rt=player; //NOT ALLOWED
RatedPlayer pr=&player; //NOT ALLOWED
C、拓展:如果基类引用或基类指针作为函数形参,实参可以是派生类对象吗?可以是基类对象吗?(B解释:可以)
如函数:void Show(const TableTennisPlayer &rt); //rt是基类引用,则:
TableTennisPlayer player1(“Tara”,“Boomdea”,false);
RatedPlayer rplayer1(1140,“Malcony”,“Duck”,true);
Show(player1); //ALLOWED
Show(rplayer1); //ALLOWED
如函数void Display(const TableTennis pt); 则:
Display(&player1);
Display(&rplayer1);
第二十讲 多态公有继承
*三、多态公有继承
前述的派生类中,对基类的公有方法未做任何修改,但有时同一个公有方法,在基类中和派生类中的行为是不同的,也就是调用同一个方法时,执行什么操作取决于是基类对象调用还是派生类对象调用,这种不同的上下文(不同的对象)下调用同一名称的函数具有不同的功能,叫多态。其实机制有两种:
在派生类中重新定义基类方法
使用虚方法
