• C++之继承


    1、代码重用

    组合:就是将一个类A作为另一个类B的对象成员,以此能用到另一个类的成员函数。(前提是类B需要用到类A中的某个功能,但是如果再在类B中写,这样代码就重复了,且要维护两份相同的代码)

    2、继承

    派生类表示的事务范围,要比基类的范围要小的多。派生类更加具体。

    3、继承的语法

    4、共有、私有、保护成员

    主要取决于继承后变成什么样的属性,protected和private的区别主要是看你想不想让派生类里面能不能直接访问,也就是说这个成员要不要暴露给派生类,如果想要暴露的话,且不想在类外访问,那就声明为protected;如果不想暴露的话,那就private。

    1. #include
    2. using namespace std;
    3. class Base
    4. {
    5. public:
    6. int x_;
    7. protected:
    8. int y_;
    9. private:
    10. int z_;
    11. };
    12. class PublicInherit : public Base
    13. {
    14. public:
    15. void Test()
    16. {
    17. x_ = 10;
    18. y_ = 20;
    19. //z_ = 30; //派生类不能访问基类中的privated成员,只能在Base类中访问
    20. }
    21. private:
    22. int a_;
    23. };
    24. int main() {
    25. Base b;
    26. b.x_ = 20;
    27. //b.y_ = 22; // 类外不能访问到protected成员
    28. return 0;
    29. }

    5、默认继承保护级别

    6、接口继承与实现继承

    接口继承(公有),也就是想把这个接口(或者重写后)再暴露出来;实现继承(私有和保护),也就是想要在类中直接调用,不暴露出来。

    7、继承与重定义

    overload:重载,只在作用域中相同的情况下

    overwrite:重写或者重定义:针对于类的继承,函数名相同(参数相不相同都是重写,会隐藏基类中的函数)

    override:覆盖:必须要有virtual虚函数才可以(后续讲到多态再讲解)

    1. #include
    2. using namespace std;
    3. class Base
    4. {
    5. public:
    6. Base() : x_(0)
    7. {
    8. }
    9. int GetBaseX() const
    10. {
    11. return x_;
    12. }
    13. void show()
    14. {
    15. cout << "call Base::show" << endl;
    16. }
    17. int x_;
    18. };
    19. class Derived : public Base
    20. {
    21. public:
    22. Derived() : x_(0)
    23. {
    24. }
    25. int GetDerivedX() const
    26. {
    27. return x_;
    28. }
    29. void show(int n)
    30. {
    31. cout << "call Derived::show n" << endl;
    32. }
    33. void show()
    34. {
    35. cout << "call Derived::show" << endl;
    36. }
    37. int x_; // 如果有重新定义基类的数据成员,那么基类的数据就会被隐藏
    38. };
    39. int main() {
    40. Derived d;
    41. d.x_ = 20;
    42. cout << d.GetBaseX() << endl;
    43. cout << d.GetDerivedX() << endl;
    44. d.show(); // 对于子类来说,show这个接口已经被重定义成show(int n),所以这样调用会报错,相当于子类没有这个函数,所以子类中需要加上show()
    45. // 继承关系中,不存在重载,只有重写或者覆盖。因为重载只在作用域相同的情况下才行(就像Derived中的两个show)
    46. // 如果子类对象想要调用被隐藏的数据,那就这样调用
    47. d.Base::x_ = 30;
    48. d.Base::show();
    49. return 0;
    50. }

    8、继承与组合

    9、不能自动继承的成员函数

    10、继承与构造函数

    必须使用构造函数初始化列表初始化的有如下:

            1、const成员:因为是常量,所以必须一开始就初始化。为什么不能在构造函数体里面初始化?因为如果在构造函数体里面初始化,本质上不叫初始化,叫做赋值。

            2、引用成员:因为引用在定义的时候,也必须直接初始化。如果在构造函数体中初始化,理由如上。

            3、类的对象成员没有默认构造函数的时候:只能在类的构造函数初始化列表中调用该对象的构造函数进程初始化

    派生类对象构造次序:

    先调用基类的对象成员的构造函数、然后再调用基类构造函数、再然后是派生类的对象成员的构造函数、最终是派生类自身的构造函数(总的来说,构造一个类,先对象成员,再构造函数)

    1. #include
    2. using namespace std;
    3. class ObjectB
    4. {
    5. public:
    6. ObjectB(int objb) : objb_(objb)
    7. {
    8. cout << "ObjectB ..." << endl;
    9. }
    10. ~ObjectB()
    11. {
    12. cout << "~ObjectB ... " << endl;
    13. }
    14. int objb_;
    15. };
    16. class ObjectD
    17. {
    18. public:
    19. ObjectD(int objd) : objd_(objd)
    20. {
    21. cout << "ObjectD ..." << endl;
    22. }
    23. ~ObjectD()
    24. {
    25. cout << "~ObjectD ... " << endl;
    26. }
    27. int objd_;
    28. };
    29. class Base
    30. {
    31. public:
    32. Base(int b) : b_(b), objb_(111)
    33. {
    34. cout << "Base ..." << endl;
    35. }
    36. // 拷贝构造
    37. Base(const Base& other) : objb_(other.objb_), b_(other.b_)
    38. {
    39. }
    40. ~Base()
    41. {
    42. cout << "~Base ... " << endl;
    43. }
    44. int b_;
    45. ObjectB objb_; // 这边要调用ObjectB的默认构造函数,如果没有编译失败。要么增加默认构造函数,要么在构造函数初始化列表中初始化
    46. };
    47. class Derived : public Base
    48. {
    49. public:
    50. Derived(int d, int b) : d_(d), Base(b), objd_(222) // 这边会调用基类的默认构造函数,但是此时没有默认的构造函数,所以会报错;所以这边要加上Base(10)去调用指定的构造函数
    51. {
    52. cout << "Derived ..." << endl;
    53. }
    54. // 拷贝构造
    55. Derived(const Derived& other) : d_(other.d_), objd_(other.objd_), Base(other) // 因为这是拷贝构造,而且又是子类,所以必须把基类也一起拷贝,也就是Base(other),这个很经常遗漏!
    56. {
    57. }
    58. ~Derived()
    59. {
    60. cout << "~Dericed ..." << endl;
    61. }
    62. int d_;
    63. ObjectD objd_;
    64. };
    65. int main() {
    66. Derived d(100, 200);
    67. cout << d.b_ << " " << d.d_ << endl;
    68. Base b1(100);
    69. Base b2(b1);
    70. cout << b2.b_ << endl;
    71. Derived d2(d);
    72. return 0;
    73. }
    74. // 输出
    75. ObjectB ...
    76. Base ...
    77. ObjectD ...
    78. Derived ...
    79. 200 100
    80. ObjectB ...
    81. Base ...
    82. 100
    83. ~Dericed ...
    84. ~ObjectD ...
    85. ~Base ...
    86. ~ObjectB ...
    87. ~Base ...
    88. ~ObjectB ...
    89. ~Base ...
    90. ~ObjectB ...
    91. ~Dericed ...
    92. ~ObjectD ...
    93. ~Base ...
    94. ~ObjectB ...

    11、友元关系与继承

    12、静态成员与继承、

    静态成员无所谓继承,因为静态成员是共享的,在内存中只有一份。

    1. #include
    2. using namespace std;
    3. class Base
    4. {
    5. public:
    6. static int b_;
    7. };
    8. int Base::b_ = 100;
    9. class Derived : public Base
    10. {
    11. };
    12. int main() {
    13. Base b;
    14. cout << Base::b_ << endl;
    15. cout << b.b_ << endl; // 不推荐用对象进行访问,会让人误以为是普通成员函数
    16. Derived d;
    17. cout << Derived::b_ << endl;
    18. cout << d.b_ << endl;
    19. return 0;
    20. }
    21. // 输出
    22. 100
    23. 100
    24. 100
    25. 100

    13、转换与继承

    14、派生类到基类的转换

    static_cast:用于编译器认可的静态转换。比如说从char 到int。从double到int。或者具有转换构造函数。或者重载了类型转换运算符。

    reinterpret_cast:用于编译器不认可的静态转换。从int* 转为int,且在转型的时候,不做任何对其。

    const_cast:去除常量性

    dynamic_cast:用于动态转换。安全的向下转型。常用于多态

    1. #include
    2. #include
    3. using namespace std;
    4. class Employee
    5. {
    6. public:
    7. Employee(const string& name, const int age, const int depno) : name_(name), age_(age), depno_(depno)
    8. {
    9. }
    10. private:
    11. string name_;
    12. int age_;
    13. int depno_;
    14. };
    15. class Manager : public Employee
    16. {
    17. public:
    18. Manager(const string& name, const int age, const int depno, int level)
    19. : Employee(name, age, depno), level_(level) // 这边必须要对基类进行初始化列表,指定调用哪个基类的构造函数,如果不指定的话,那就是调用默认构造函数,当前例子没有构造函数
    20. {
    21. }
    22. private:
    23. int level_;
    24. };
    25. class Manager2 : private Employee
    26. {
    27. public:
    28. Manager2(const string& name, const int age, const int depno, int level)
    29. : Employee(name, age, depno), level_(level) // 这边必须要对基类进行初始化列表,指定调用哪个基类的构造函数,如果不指定的话,那就是调用默认构造函数,当前例子没有构造函数
    30. {
    31. }
    32. private:
    33. int level_;
    34. };
    35. int main() {
    36. // public继承举例:
    37. Employee e1("niuma", 25, 1);
    38. Manager m1("ziben", 40, 1, 5);
    39. Employee* pe;
    40. Manager* pm;
    41. pe = &e1; // 基类指针指向基类对象
    42. pm = &m1; // 派生类指针指向派生类对象
    43. // 就好比如,人是一个基类,教师是派生类,教师(派生类)是一个人(基类),但是,人(基类)不一定是一个教师
    44. pe = &m1; // 派生类对象指针可以转化成基类对象指针。将派生类对象看成是基类对象
    45. //pm = &e1; // 基类指针无法转化为派生类指针,无法将基类对象看成是派生类对象
    46. e1 = m1; // 派生类对象可以转化成基类对象。将派生类对象看成是基类对象
    47. //会产生切割(派生类特有成员消失;level_消失)。object slicing
    48. // private继承举例:
    49. Manager2 m2("aaa", 35, 2, 3);
    50. Manager2* pm2;
    51. pm2 = &m2; // 派生类指针指向派生类对象
    52. //pe = pm2; // 私有或保护继承的时候,派生类对象指针不能自动转化为基类对象指针
    53. pe = reinterpret_cast(pm2);
    54. //e1 = m2; // 派生类对象无法转化为基类对象
    55. // e1 = (Employee)m2; // 即使是强制转化也不行
    56. return 0;
    57. }

    15、基类到派生类的转换

    1. #include
    2. #include
    3. using namespace std;
    4. class Manager;
    5. class Employee
    6. {
    7. public:
    8. Employee(const string& name, const int age, const int depno) : name_(name), age_(age), depno_(depno)
    9. {
    10. }
    11. operator Manager();
    12. private:
    13. string name_;
    14. int age_;
    15. int depno_;
    16. };
    17. class Manager : public Employee
    18. {
    19. public:
    20. Manager(const string& name, const int age, const int depno, int level)
    21. : Employee(name, age, depno), level_(level) // 这边必须要对基类进行初始化列表,指定调用哪个基类的构造函数,如果不指定的话,那就是调用默认构造函数,当前例子没有构造函数
    22. {
    23. }
    24. //构造函数只有一个参数,也称为转换构造函数;将其他类型转化为Manager类型
    25. // 从语法上来演示基类对象可以转化为派生类对象,没有实际意义
    26. Manager(const Employee& other) : Employee(other), level_(-1)
    27. {
    28. }
    29. private:
    30. int level_;
    31. };
    32. Employee::operator Manager() {
    33. return Manager(name_, age_, depno_, -1);
    34. }
    35. int main() {
    36. // public继承举例:
    37. Employee e1("niuma", 25, 1);
    38. Manager m1("ziben", 40, 1, 5);
    39. // 基类对象转换成派生类对象
    40. // 方式1:使用转换构造函数(需提供转换构造函数),将其他类型转化为类类型
    41. m1 = e1;
    42. // 方式2:重载类型转换运算符:将类类型转换为其他类型
    43. // 也就是在Employee中增加 operator Manager()
    44. return 0;
    45. }

    14、多重继承

    1. #include
    2. using namespace std;
    3. class Furniture
    4. {
    5. public:
    6. Furniture(int weight) : weight_(weight)
    7. {
    8. }
    9. int weight_;
    10. };
    11. class Bed : public Furniture
    12. {
    13. public:
    14. Bed(int weight) : Furniture(weight)
    15. {
    16. }
    17. void Sleep()
    18. {
    19. cout << "Sleep .. " << endl;
    20. }
    21. };
    22. class Sofa : public Furniture
    23. {
    24. public:
    25. Sofa(int weight) : Furniture(weight)
    26. {
    27. }
    28. void WatchTV()
    29. {
    30. cout << "Watch TV ..." << endl;
    31. }
    32. };
    33. class SofaBed : public Bed, public Sofa
    34. {
    35. public:
    36. SofaBed(int weight) : Bed(weight), Sofa(weight)
    37. {
    38. FoldIn();
    39. }
    40. void FoldOut()
    41. {
    42. cout << "FoldOut ..." << endl;
    43. }
    44. void FoldIn()
    45. {
    46. cout << "FoldIn ..." << endl;
    47. }
    48. };
    49. int main() {
    50. SofaBed sofabed(10);
    51. //sofabed.weight_ = 10; // 不能这样访问,因为编译器没办法确定是哪个基类的weight_;只能用下面的方式访问重名的成员
    52. sofabed.Bed::weight_ = 10;
    53. sofabed.Sofa::weight_ = 20;
    54. // 就算是将weight_写到Furniture类中,但是由于SofaBed继承了两个类,所以SofaBed里面还是会有两个weight_;我们希望是只有一个weight_
    55. // 这个时候就只能用虚继承来解决
    56. return 0;
    57. }

    15、虚继承与虚基类

    1. #include
    2. using namespace std;
    3. class Furniture
    4. {
    5. public:
    6. // Furniture(int weight) : weight_(weight)
    7. // {
    8. //
    9. // }
    10. int weight_;
    11. };
    12. class Bed : virtual public Furniture
    13. {
    14. public:
    15. // Bed(int weight) : Furniture(weight)
    16. // {
    17. //
    18. // }
    19. void Sleep()
    20. {
    21. cout << "Sleep .. " << endl;
    22. }
    23. };
    24. class Sofa : virtual public Furniture
    25. {
    26. public:
    27. // Sofa(int weight) : Furniture(weight)
    28. // {
    29. //
    30. // }
    31. void WatchTV()
    32. {
    33. cout << "Watch TV ..." << endl;
    34. }
    35. };
    36. class SofaBed : public Bed, public Sofa
    37. {
    38. public:
    39. // SofaBed(int weight) : Bed(weight), Sofa(weight)
    40. // {
    41. // FoldIn();
    42. // }
    43. void FoldOut()
    44. {
    45. cout << "FoldOut ..." << endl;
    46. }
    47. void FoldIn()
    48. {
    49. cout << "FoldIn ..." << endl;
    50. }
    51. };
    52. int main() {
    53. SofaBed sofabed;
    54. sofabed.weight_ = 10;
    55. return 0;
    56. }

    16、虚基类及其派生类构造函数

    所有派生类,如果有虚继承一个基类,则所有的派生类中的构造函数的初始化列表中,必须对这个虚基类的构造函数初始化。

    如果有Furniture(weight)对虚基类的构造函数进行初始化,则Bed和Sofa中的构造函数中的构造函数初始化列表中对虚基类的初始化Furniture(weight)就会被忽略,这样就变成了虚基类只被构造一次
    1. #include
    2. using namespace std;
    3. class Furniture
    4. {
    5. public:
    6. Furniture(int weight) : weight_(weight)
    7. {
    8. cout << "Furniture(int weight) : weight_(weight) ..." << endl;
    9. }
    10. ~Furniture()
    11. {
    12. cout << "~Furniture() ..." << endl;
    13. }
    14. int weight_;
    15. };
    16. class Bed : virtual public Furniture
    17. {
    18. public:
    19. Bed(int weight) : Furniture(weight)
    20. {
    21. cout << "Bed(int weight) ..." << endl;
    22. }
    23. void Sleep()
    24. {
    25. cout << "Sleep .. " << endl;
    26. }
    27. ~Bed()
    28. {
    29. cout << "~Bed() ..." << endl;
    30. }
    31. };
    32. class Sofa : virtual public Furniture
    33. {
    34. public:
    35. Sofa(int weight) : Furniture(weight)
    36. {
    37. cout << "Sofa(int weight) ..." << endl;
    38. }
    39. void WatchTV()
    40. {
    41. cout << "Watch TV ..." << endl;
    42. }
    43. ~Sofa()
    44. {
    45. cout << "~Sofa() ..." << endl;
    46. }
    47. };
    48. class SofaBed : public Bed, public Sofa
    49. {
    50. public:
    51. // 下面如果有Furniture(weight)对虚基类的构造函数进行初始化,则Bed和Sofa中的构造函数中的构造函数初始化列表中对虚基类的初始化Furniture(weight)就会被忽略,这样就变成了虚基类只被构造一次
    52. SofaBed(int weight) : Bed(weight), Sofa(weight), Furniture(weight)
    53. {
    54. cout << "SofaBed(int weight) : Bed(weight), Sofa(weight), Furniture(weight) ..." << endl;
    55. FoldIn();
    56. }
    57. void FoldOut()
    58. {
    59. cout << "FoldOut ..." << endl;
    60. }
    61. void FoldIn()
    62. {
    63. cout << "FoldIn ..." << endl;
    64. }
    65. ~SofaBed()
    66. {
    67. cout << "~SofaBed() ..." << endl;
    68. }
    69. };
    70. int main() {
    71. SofaBed sofabed(10);
    72. sofabed.WatchTV();
    73. sofabed.FoldOut();
    74. sofabed.Sleep();
    75. return 0;
    76. }
    77. // 输出
    78. Furniture(int weight) : weight_(weight) ...
    79. Bed(int weight) ...
    80. Sofa(int weight) ...
    81. SofaBed(int weight) : Bed(weight), Sofa(weight), Furniture(weight) ...
    82. FoldIn ...
    83. Watch TV ...
    84. FoldOut ...
    85. Sleep ..
    86. ~SofaBed() ...
    87. ~Sofa() ...
    88. ~Bed() ...
    89. ~Furniture() ...

    17、虚继承对C++内存模型造成的影响

    1、类/对象大小计算

    2、虚基类表

    当你一个类虚继承自另一个类的时候,这个类的对象的内存中就会多一个虚基类表指针,指向了一个虚基类表。这个虚基类表中存放两个值,该类地址与该对象地址的偏移量和该类地址与虚基类表指针的偏移量。用处在于通过偏移量来获取到基类的成员。

    其实就是为了虚基类成员的共享,一般放在内存的末尾,所以需要记录偏移量。

    比如这个例子:

    B1中的vbptr指向的第一个值应该是B1类的地址和DD的对象d之间的偏移量,因为恰好d内存中的第一个位置是B1,所以偏移量是0

    B1中的vbptr指向的第二个值应该是B类地址和BB的地址之间的偏移量。

    这时候,d.B1::bb,其实是通过d的地址,先加上第一个偏移量,先找到B1的地址,然后再加上第二个偏移量,找到bb。

    同理,B2中的vbptr存的第一个是DD的对象地址与B2内存的初始地址的偏移量,第二个是B2内存的初始地址与BB内存的初始地址的偏移量,这样B2才能索引到BB中的值。

    d.B2::bb,就是d的地址加上第一个偏移量索引到B2的位置,然后再加上第二个偏移量,索引到bb。

    1. #include
    2. using namespace std;
    3. class BB
    4. {
    5. public:
    6. int bb_;
    7. };
    8. class B1 : virtual public BB
    9. {
    10. public:
    11. int b1_;
    12. };
    13. class B2 : virtual public BB
    14. {
    15. public:
    16. int b2_;
    17. };
    18. class DD : public B1, public B2
    19. {
    20. public:
    21. int dd_;
    22. };
    23. int main() {
    24. cout << sizeof(BB) << endl;
    25. cout << sizeof(B1) << endl;
    26. cout << sizeof(DD) << endl;
    27. B1 b1;
    28. long** p;
    29. cout << &b1 << endl;
    30. cout << &b1.b1_ << endl;
    31. cout << &b1.bb_ << endl;
    32. p = (long**)&b1;
    33. cout << p[0][0] << endl;
    34. cout << p[0][1] << endl;
    35. return 0;
    36. }
  • 相关阅读:
    从零开始学习线性回归:理论、实践与PyTorch实现
    regexp_split_to_table,regexp_split_to_array,array,unnest 使用
    【STL***vector容器三】
    会议OA之待开会议&所有会议
    Maven进阶-依赖管理
    【数据结构】二叉搜索树
    【重装系统的血泪史
    解决【无法处理文件xxx,因为它位于 Internet 或受限区域中,或者文件上具有 Web 标记。要想处理这些文件,请删除 Web 标记】问题
    向量数据库
    【Java 进阶篇】JQuery 遍历 —— For 循环的奇妙之旅
  • 原文地址:https://blog.csdn.net/CSDN_HZW/article/details/139316695