多重继承中派生类的构造函数和析构函数
2016-04-28 09:19
281 查看
#include<iostream> using namespace std; class Base1 { public: Base1(int i) { cout<<"Constructing Base1 "<<i<<endl; } }; class Base2 { public: Base2(int j) { cout<<"Constructing Base2 "<<j<<endl; } }; class Base3 { public: Base3() { cout<<"Constructing Base3 *"<<endl; } }; class Derived:public Base1,public Base2,public Base3 { public: Derived(int a,int b,int c,int d):Base1(a),Base2(b),number1(c),number2(d){} private: Base1 number1; Base2 number2; Base3 number3; }; int main() { Derived obj(1,2,3,4); return 0; }
运行结果:
心得:
1、调用基类构造函数,调用顺序按照他们被继承时声明的顺序(从左向右)
2、对本类成员初始化列表中的基本类型成员和对象成员进行初始化,初始化的顺序按照他们在类中声明的顺序,对象成员初始化是自动调用对象所属类的构造函数完成。
3、执行派生类的构造函数体的内容。
对比:
#include<iostream> using namespace std; class Base1 { public: Base1(int i) { cout<<"Constructing Base1 "<<i<<endl; } }; class Base2 { public: Base2(int j) { cout<<"Constructing Base2 "<<j<<endl; } }; class Base3 { public: Base3() { cout<<"Constructing Base3 *"<<endl; } }; class Derived:public Base2,public Base1,public Base3 { public: Derived(int a,int b,int c,int d):Base1(a),Base2(b),number1(c),number2(d){} private: Base1 number1; Base2 number2; Base3 number3; }; int main() { Derived obj(1,2,3,4); return 0; }
运行结果:
相关文章推荐
- iOS开发之运动事件和远程控制
- C#编写的基于VLC的播放器
- php编程中require和include多层嵌套导致文件找不到的错误
- 第九周阅读项目-(3)
- HDU 5672 String (尺取法)
- 单链表的选择排序
- 关于oracle 11g导出的dmp文件无法导入10g的问题
- 第9周项目3:人数不定的工资类
- ubuntu搭建go环境
- wordpress 维护技巧以及一些插件
- spring-源码学习-1
- ZOJ 3940
- ZOJ 3607-Lazier Salesgirl-贪心
- 【OpenSSL】base64 with BIO filter
- 人才流失很大程度取决于中层领导
- esp8266应用笔记
- Knapsack Problem|背包问题
- 生活太糙(笑
- 九大排序算法-大数据
- load 和 initialize 的区别