C++ Primer Plus 学习笔记 第十三章 类继承

秒速五厘米 2022-01-26 09:47 332阅读 0赞

继承格式

class A :parent B

{

}

派生类要有自己的构造函数

可以根据需要添加额外的数据成员和成员函数

2019052818264935.png

20190528182655636.png

20190528182704847.png

2019052818271588.png

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NvdWx3eWI_size_16_color_FFFFFF_t_70

继承的例子:

tabtenn1.h

  1. #ifndef TABTENN1_H_
  2. #define TABTENN1_H_
  3. #include <string>
  4. using std::string;
  5. class TableTennisPlayer
  6. {
  7. private:
  8. string firstname;
  9. string lastname;
  10. bool hasTable;
  11. public:
  12. TableTennisPlayer(const string & fn = "none",
  13. const string & ln = "none", bool ht = false);
  14. void Name() const;
  15. bool HasTable() const {return hasTable;}
  16. void ResetTable(bool v) {hasTable = v;}
  17. };
  18. class RatedPlayer : public TableTennisPlayer
  19. {
  20. private:
  21. unsigned int rating;
  22. public:
  23. RatedPlayer (unsigned int r = 0, const string & fn = "none",
  24. const string & ln = "none", bool ht = false);
  25. RatedPlayer (unsigned int r, const TableTennisPlayer & tp);
  26. unsigned int Rating() const { return rating;}
  27. void ResetTable (unsigned int r) {rating = r;}
  28. };
  29. #endif

tabtenn1.cpp

  1. #include "tabtenn1.h"
  2. #include <iostream>
  3. TableTennisPlayer::TableTennisPlayer(const string & fn,
  4. const string & ln, bool ht) : firstname(fn),
  5. lastname(ln), hasTable(ht) {}
  6. void TableTennisPlayer::Name() const
  7. {
  8. std::cout << lastname << ", " << firstname;
  9. }
  10. RatedPlayer::RatedPlayer(unsigned int r, const string & fn,
  11. const string & ln, bool ht) : TableTennisPlayer(fn, ln, ht)
  12. {
  13. rating = r;
  14. }
  15. RatedPlayer::RatedPlayer(unsigned int r, const TableTennisPlayer & tp)
  16. : TableTennisPlayer(tp), rating(r)
  17. {
  18. }

usett1.cpp

  1. #include <iostream>
  2. #include "tabtenn1.h"
  3. int main(void)
  4. {
  5. using std::cout;
  6. using std::endl;
  7. TableTennisPlayer player1("Tara", "Boomdea", false);
  8. RatedPlayer rplayer1(1140, "Mallory", "Duck", true);
  9. rplayer1.Name();
  10. if (rplayer1.HasTable())
  11. cout << ": has a table.\n";
  12. else
  13. cout << ": hasn't a table.\n";
  14. player1.Name();
  15. if (player1.HasTable())
  16. cout << ": has a table";
  17. else
  18. cout << ": hasn't a table.\n";
  19. cout << "Name: ";
  20. rplayer1.Name();
  21. cout << "; Rating: " << rplayer1.Rating() << endl;
  22. RatedPlayer rplayer2(1212, player1);
  23. cout << "Name: ";
  24. rplayer2.Name();
  25. cout << "; Rating: " << rplayer2.Rating() << endl;
  26. return 0;
  27. }

其他:

派生类可以用非私有的基类方法

基类指针或引用可以自动转换成指向派生类的对象。 BUT 基类指针或引用依然只能使用基类的函数 而不能调用派生类的自己的函数

不能反过来 也就是说 不能将基类对象的指针或引用赋值给派生类的指针或引用

20190528184054158.png

关于将派生类对象传递给基类对象初始化

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NvdWx3eWI_size_16_color_FFFFFF_t_70 1

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NvdWx3eWI_size_16_color_FFFFFF_t_70 2

也就是说 派生类传递到基类对象中进行初始化 编译器会调用相应的复制构造函数 然后将基类涉及到的形参传递过去。不涉及的就丢给派生类的构造函数去处理。

完结

发表评论

表情:
评论列表 (有 0 条评论,332人围观)

还没有评论,来说两句吧...

相关阅读