多重继承的构造函数和析构函数的执行顺序:
- //此处不做注释和说明了,看程序能读懂的
- #include <iostream>
- using namespace std;
- class base1
- {
- private:
- int x1;
- public:
- base1(int y1)
- {
- x1 = y1;
- cout<<"基类1构造函数执行..."<<"x1= "<<x1<<endl;
- }
- ~base1(){cout<<"base1 析构了...\n";}
- };
- class base2
- {
- private:
- int x2;
- public:
- base2(int y2)
- {
- x2 = y2;
- cout<<"基类2构造函数执行..."<<"x2= "<<y2<<endl;
- }
- ~base2(){cout<<"base2 析构了...\n";}
- };
- class base3
- {
- private:
- int x3;
- public:
- base3(){cout<<"构造基类3...\n";}
- ~base3()
- {
- cout<<"基类3析构了...\n";
- }
- };
- //多重继承派生类的构造函数是按照声明的顺序执行的...
- class derived:public base2,public base1,public base3
- {
- private:
- base1 obj1;
- base2 obj2;
- base3 obj3;
- public:
- derived(int x,int y,int z,int v):base1(x),base2(y),obj1(z),obj2(v)
- {
- cout<<"派生类构造函数执行...\n";
- }
- ~derived(){cout<<"派生类析构函数执行...\n";}
- };
- int main()
- {
- derived ob(1,2,3,4);
- return 0;
- }
转载于:https://blog.51cto.com/nojob/318434