• 第46课 继承中的构造与析构


    思考:

    如何初始化父类成员?

    父类构造函数和子类构造函数有什么关系?

    子类对象的构造:

    子类构造函数对继承而来的成员进行初始化有两种方式:

    1、直接通过初始化列表或者赋值的方式进行初始化

    2、调用父类构造函数进行初始化

    父类构造函数在子类中的调用方式:

    显式调用只能在初始化列表进行。

    如下:

     示例程序:

    第30行会调用子类的无参构造函数Child(),而这个构造函数会默认调用父类的无参构造函数,因为父类没有默认构造函数,因此报错。

     在父类中加上无参构造函数即可:

    继续完善:

     1 #include <iostream>
     2 #include <string>
     3 
     4 using namespace std;
     5 
     6 class Parent 
     7 {
     8 public:
     9     Parent()
    10     {
    11         cout << "Parent()" << endl;
    12     }
    13     Parent(string s)
    14     {
    15         cout << "Parent(string s) : " << s << endl;
    16     }
    17 };
    18 
    19 class Child : public Parent
    20 {
    21 public:
    22     Child()
    23     {
    24         cout << "Child()" << endl;
    25     }
    26     Child(string s) : Parent(s)
    27     {
    28         cout << "Child(string s) : " << s << endl;
    29     }
    30 };
    31 
    32 int main()
    33 {       
    34     Child c; 
    35     Child cc("cc");
    36     
    37     return 0;
    38 }

    35行会触发26行的子类构造函数,在调用子类的构造函数前,先调用父类的Parent(string s)这个构造函数。

    输出结果如下:

     子类对象的构造:

    深度解析:

     1 #include <iostream>
     2 #include <string>
     3 
     4 using namespace std;
     5 
     6 class Object
     7 {
     8 public:
     9     Object(string s)
    10     {
    11         cout << "Object(string s) : " << s << endl;
    12     }
    13 };
    14 
    15 class Parent : public Object
    16 {
    17 public:
    18     Parent() : Object("Default")
    19     {
    20         cout << "Parent()" << endl;
    21     }
    22     Parent(string s) : Object(s)
    23     {
    24         cout << "Parent(string s) : " << s << endl;
    25     }
    26 };
    27 
    28 class Child : public Parent
    29 {
    30     Object mO1;
    31     Object mO2;
    32 public:
    33     Child() : mO1("Default 1"), mO2("Default 2")
    34     {
    35         cout << "Child()" << endl;
    36     }
    37     Child(string s) : Parent(s), mO1(s + " 1"), mO2(s + " 2")
    38     {
    39         cout << "Child(string s) : " << s << endl;
    40     }
    41 };
    42 
    43 int main()
    44 {       
    45     Child cc("cc");
    46     
    47     return 0;
    48 }

    运行结果如下:

    子类对象的析构:

     示例程序:

     1 #include <iostream>
     2 #include <string>
     3 
     4 using namespace std;
     5 
     6 class Object
     7 {
     8     string ms;
     9 public:
    10     Object(string s)
    11     {
    12         cout << "Object(string s) : " << s << endl;
    13         ms = s;
    14     }
    15     ~Object()
    16     {
    17         cout << "~Object() : " << ms << endl;
    18     }
    19 };
    20 
    21 class Parent : public Object
    22 {
    23     string ms;
    24 public:
    25     Parent() : Object("Default")
    26     {
    27         cout << "Parent()" << endl;
    28         ms = "Default";
    29     }
    30     Parent(string s) : Object(s)
    31     {
    32         cout << "Parent(string s) : " << s << endl;
    33         ms = s;
    34     }
    35     ~Parent()
    36     {
    37         cout << "~Parent() : " << ms << endl;
    38     }
    39 };
    40 
    41 class Child : public Parent
    42 {
    43     Object mO1;
    44     Object mO2;
    45     string ms;
    46 public:
    47     Child() : mO1("Default 1"), mO2("Default 2")
    48     {
    49         cout << "Child()" << endl;
    50         ms = "Default";
    51     }
    52     Child(string s) : Parent(s), mO1(s + " 1"), mO2(s + " 2")
    53     {
    54         cout << "Child(string s) : " << s << endl;
    55         ms = s;
    56     }
    57     ~Child()
    58     {
    59         cout << "~Child() " << ms << endl;
    60     }
    61 };
    62 
    63 int main()
    64 {       
    65     Child cc("cc");
    66     
    67     cout << endl;
    68     
    69     return 0;
    70 }

    执行结果如下:

    小结:

  • 相关阅读:
    Linuxboot:linux as UEFI,linux over UEFI
    在阿里云上安装黑苹果的一种设想
    Dsm as deepin mate(3):离线编辑初始镜像,让skynas本地验证启动安装/升级
    硬件融合的新起点:虚拟firmware,avatt的编译(2)
    将虚拟机集成在BIOS和EFI层,vavvt的编译(1)
    2013.08.19—2013.08.23周总结
    关于自我介绍
    Java入门系列:实例讲解ArrayList用法
    Hadoop文件的基本操作
    继承关系的理解
  • 原文地址:https://www.cnblogs.com/wanmeishenghuo/p/9574731.html
Copyright © 2020-2023  润新知