C++中派生类析构函数实例

派生类析构函数的执行次序和构造函数正好相反,顺序如下:

首先,派生类新增的普通成员进行处理,然后对派生类新增的对象成员进析构处理,最后对所有从基类继承来的成员进行处理。

这些工作分别是执行

1。 派生类析构函数。

2。 调用派生类对象成员所在的类的析构函数。

3。调用基类析构函数

--------------------------------------------------

/*
 * File:   main.cpp
 * Author: yubao
 *
 * Created on May 31, 2009, 8:12 AM
 */

#include <iostream>
using namespace std;
class B1
{
public :
    B1(int i){cout<<"constructing B1   "<<i<<endl;} //基类构造函数,有参数
    ~B1(){cout<<"destructing B1"<<endl;}
};
class B2
{
public :
    B2(int j){ cout<<"constrcuting B2   "<<j<<endl;}//基类构造函数,有参数
    ~B2(){cout<<"destructing B2"<<endl;}
};
class B3
{
public :
    B3(){cout<<"constructing B3 * "<<endl;}//基类构造函数,无参数
    ~B3(){cout<<"destructing B3"<<endl;}
};
class C:public B2, public B1, public B3
{
public:
    C(int a,int b,int c,int d):B1(a),memberB2(d),memberB1(c),B2(b){}//注意派生类的基类声明顺序
private :
    B1 memberB1;//注意内迁对象的声明顺序
    B2 memberB2;
    B3 memberB3;
};


/*
 *
 */
int main(int argc, char** argv) {
    C obj(1,2,3,4);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值