[关闭]
@cxm-2016 2016-09-27T04:56:47.000000Z 字数 2844 阅读 1681

C++:类继承

c++

编号:01010103011160907002
作者:陈小默
更新信息:1,修改了部分错别字。2,修改引用的显示方式

定义一个基类

从一个类派生出另外一个类的时候,原始类被称为基类,继承类被称为派生类。为说明继承关系,首先需要定义一个基类。以下定义一个乒乓球会会员。

  1. #ifndef primer_table_tennis_player_h
  2. #define primer_table_tennis_player_h
  3. #include<string>
  4. #include<iostream>
  5. using std::cout;
  6. using std::endl;
  7. using std::string;
  8. class TableTennisPlayer{
  9. private:
  10. string _name;
  11. bool _hasTable;
  12. public:
  13. TableTennisPlayer(const string & name="anon",bool hasTable=false);
  14. void name() const;
  15. bool hasTable() const{return _hasTable;};
  16. void resetTable(bool hasTable){_hasTable=hasTable;};
  17. };
  18. TableTennisPlayer::TableTennisPlayer(const string & name,bool hasTable):_name(name),_hasTable(hasTable){}
  19. void TableTennisPlayer::name()const{
  20. cout<<_name<<" : "<<endl;
  21. }
  22. #endif

这里测试一下

  1. #include"stdafx.h"
  2. #include"table_tennis_player.h"
  3. int main(int argc, const char * argv[]) {
  4. TableTennisPlayer jack("Jack",false),sam("Sam",true);
  5. jack.name();
  6. cout<<(jack.hasTable()?"has a table":"hasn't a table")<<endl;
  7. sam.name();
  8. cout<<(sam.hasTable()?"has a table":"hasn't a table")<<endl;
  9. return 0;
  10. }

运行结果

Jack :
hasn't a table
Sam :
has a table

派生一个类

现在需要这样一个类,它能够包含成员在比赛中的比分。现在将要从原有的TableTennisPlayer派生出一个类。

如何声明一个继承

  1. class RatedPlayer : public TableTennisPlayer {...};

冒号支出RatedPlayer类的基类是TableTennisPlayer类。public表明了这是一个公有基类,这个类被称为公有派生类。使用公有派生,基类的公有成员将会称为派生类的公有成员;基类的私有成员也是派生类的一部分,但是派生类不能直接访问。

继承中应该添加什么?

派生类的构造函数

派生类不能访问基类的私有成员,而必须通过基类公有方法进行访问。具体的说,派生类的构造函数必须使用基类的构造函数。创建派生类时,程序首先创造基类对象。从概念上讲,这意味着基类对象应当在程序进入派生类的构造函数之前被创建。下面是一个构造函数的代码:

  1. RatedPlayer::RatedPlayer(unsigned rating,const string name,bool hasTable):TableTennisPlayer(name,hasTable){
  2. _rating = rating;
  3. }

如果我们没有使用冒号显式的调用基类的构造方法,函数将调用基类默认的构造方法,也就是说在没有显式调用基类构造方法的情况下,基类必须提供一个默认的构造函数。

有关派生类构造函数:

注意:派生类创建对象的时候,程序首先调用基类的构造函数,然后再调用派生类的构造函数。基类构造函数负责初始化继承的数据成员;派生类的构造函数用于初始化新增的数据成员。派生类对象过期时,程序首先调用派生类的析构函数,然后再调用基类的析构函数。

使用派生类

  1. #ifndef primer_rated_player_h
  2. #define primer_rated_player_h
  3. #include"table_tennis_player.h"
  4. class RatedPlayer : public TableTennisPlayer{
  5. private:
  6. unsigned _rating;
  7. public:
  8. RatedPlayer(unsigned rating = 0,const string & name = "anon",bool hasTable = false);
  9. RatedPlayer(unsigned rating,const TableTennisPlayer &player);
  10. unsigned rating() const {return _rating;};
  11. void resetRating(unsigned rating){_rating=rating;};
  12. };
  13. RatedPlayer::RatedPlayer(unsigned rating,const string & name,bool hasTable):TableTennisPlayer(name,hasTable),_rating(rating){}
  14. RatedPlayer::RatedPlayer(unsigned rating,const TableTennisPlayer &player):TableTennisPlayer(player),_rating(rating){}
  15. #endif

测试运行

  1. #include"stdafx.h"
  2. #include"rated_player.h"
  3. int main(int argc, const char * argv[]) {
  4. TableTennisPlayer jack("Jack",true);
  5. RatedPlayer p_jack(12,jack);
  6. p_jack.name();
  7. cout<<"rating is "<<p_jack.rating()<<endl;
  8. return 0;
  9. }

运行结果

Jack :
rating is 12

派生类和基类之间的特殊关系

派生类可以使用基类中非私有的方法。并且其多态属性导致基类指针可以指向派生类对象;但是派生类指针不能自动指向基类对象。[1]


[1] Stephen Prata.C++ Primer Plus 6th.人民邮电出版社.2016.3 481~489
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注