zoukankan      html  css  js  c++  java
  • 【转】C++基类和派生类的构造函数和析构函数的调用

    C++基类和派生类的构造函数和析构函数的调用

    1.调用顺序

      当创建一个派生类的对象时,系统首先自动创建一个基类对象,也就是说,在调用派生类构造函数创建派生类对象之前,系统首先调用基类的构造函数创建基类对象。当派生类对象生命期结束时,首先调用派生类的析构函数,然后调用基类的析构函数。

      所以,构造函数:基类->派生类;析构函数:派生类->基类。

      示例:

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class A
     5 {
     6 public:
     7     A()
     8     {
     9         cout << "Based class A constructor is called" << endl;
    10     }
    11     ~A()
    12     {
    13         cout << "Based class A destructor is called" << endl;
    14     }
    15 };
    16 class B :public A
    17 {
    18 public://不写public,默认是private
    19     B()
    20     {
    21         cout << "Derived class B constructor is called" << endl;
    22     }
    23     ~B()
    24     {
    25         cout << "Derived class B destructor is called" << endl;
    26     }
    27 };
    28 
    29 int main()
    30 {
    31     B b;
    32 
    33     //system("pause");如果写这句,VS运行结果只会显示构造函数,不会显示析构函数
    34     return 0;//将上句注释,在这句加断点,手动往下执行一步,可以看到析构函数
    35 
    36 }

      【运算结果】

    2.隐式调用和显式调用

      通过派生类的构造函数调用基类的构造函数有两种方式:隐式调用和显式调用。

      所谓隐式调用,就是在派生类的构造函数中不指定对应的基类的构造函数,这个时候调用的是基类的默认构造函数(即含有默认参数值或不带参数的构造函数)

      而所谓显式调用,就是在派生类的构造函数中指定要调用的基类的构造函数,并将派生类构造函数的部分参数值传递给基类构造函数。

      注:除非基类有默认的构造函数,否则必须采用显式调用。

    2.1隐式调用示例

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class A
     5 {
     6 public:
     7     A(int x=0,int y=0)
     8     {
     9         a = x; b = y;
    10         cout << "Based class A constructor is called" << endl;
    11     }
    12     ~A()
    13     {
    14         cout << "Based class A destructor is called" << endl;
    15     }
    16 private:
    17     int a;
    18     int b;
    19 };
    20 //基类A有默认的构造函数,可以隐式调用
    21 class B :public A
    22 {
    23 public://不写public,默认是private
    24     B(int z=0)//B(int z)不能作为默认构造函数所以不可以,B()能作为默认构造函数所以也可以
    25     {
    26         c = z;
    27         cout << "Derived class B constructor is called" << endl;
    28     }
    29     ~B()
    30     {
    31         cout << "Derived class B destructor is called" << endl;
    32     }
    33 private:
    34     int c;
    35 };
    36 int main()
    37 {
    38     B b;
    39     //system("pause");如果写这句,VS运行结果只会显示构造函数,不会显示析构函数
    40     return 0;//将上句注释,在这句加断点,手动往下执行一步,可以看到析构函数
    41 
    42 }

    2.2显式调用示例

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class A
     5 {
     6 public:
     7     A(int x,int y)
     8     {
     9         a = x; b = y;
    10         cout << "Based class A constructor is called" << endl;
    11     }
    12     ~A()
    13     {
    14         cout << "Based class A destructor is called" << endl;
    15     }
    16 private:
    17     int a;
    18     int b;
    19 };
    20 //基类A没有默认的构造函数,其现有的构造函数需要传递参数
    21 //通过派生类构造函数调用A的构造函数时,必须用显式调用
    22 class B :public A
    23 {
    24 public://不写public,默认是private
    25     B(int x,int y,int z):A(x,y)//B(int z)不能作为默认构造函数所以不可以,B()能作为默认构造函数所以也可以
    26     {
    27         c = z;
    28         cout << "Derived class B constructor is called" << endl;
    29     }
    30     ~B()
    31     {
    32         cout << "Derived class B destructor is called" << endl;
    33     }
    34 private:
    35     int c;
    36 };
    37 int main()
    38 {
    39     B b(1,2,3);
    40     //system("pause");如果写这句,VS运行结果只会显示构造函数,不会显示析构函数
    41     return 0;//将上句注释,在这句加断点,手动往下执行一步,可以看到析构函数
    42 
    43 }

    ——如有不对的地方,非常欢迎给予指导!

    ——【感谢】资料来源于http://www.cnblogs.com/krisdy/archive/2009/06/11/1501390.html

  • 相关阅读:
    idea 界面乱码问题 file was loaded in the wrong enconding:"utf-8"
    svn 下载,安装,创建库,设置用户和用户组,赋权限
    eclipse文件中的乱码问题
    Eclipse安装Spring插件springsource-tool-suite
    vue.js2.0:搭建开发环境及构建项目
    排序List集合中的元素
    Java GC机制和对象Finalize方法的一点总结
    xfire发布的Webservice中Spring注入为空的解决方案
    Http报文格式学习及Get和Post主要区别总结
    [转] tomcat组成及工作原理
  • 原文地址:https://www.cnblogs.com/engraver-lxw/p/7586700.html
Copyright © 2011-2022 走看看