#include <iostream>
using namespace std;
//单继承
class Base {
int x;
int y;
public:
Base(int tox, int toy) :x(tox), y(toy) {
cout << "constructing Base" << endl;
}
~Base() {
cout << "deconstructing Base" << endl;
}
void show() const {
cout << "x==" << x << endl << "y==" << y << endl;
}
};
class Derived : public Base {
int a;
int z;
public:
Derived() :z(10), a(z), Base(z, z - 10) {
cout << "constructing Derived" << endl;
}
Derived(int toz, int toy, int tox) :z(toz), Base(toy, tox) {
cout << "constructing Derived" << endl;
}
~Derived() {
cout << "deconstructing Derived" << endl;
}
void show() {
Base::show();
cout << "z==" << z << endl << "a==" << a << endl;
}
};
//类的组合的情况,轮子和车身组成汽车
class Surface {
int x;
public:
Surface(int tox) :x(tox) {
cout << "constructing Surface" << endl;
}
virtual ~Surface() {
cout << "deconstructing Surface" << endl;
}
};
class Wheel {
int y;
public:
Wheel(int toy) :y(toy) {
cout << "constructing Wheel" << endl;
}
virtual ~Wheel() {
cout << "deconstructing Wheel" << endl;
}
};
class Car {
Wheel w1;
Surface s1;
public:
Car(int tox, int toy) : s1(tox), w1(toy) {}
virtual ~Car() {}
};
//组合结束
//多继承
class Name {
public:
string name;
public:
Name(const string& name) : name(name) {}
virtual ~Name() {}
};
class Chinese : virtual public Name {
public:
int id;
public:
Chinese(const string& name, int id) : Name(name), id(id) {
cout << "constructing Chinese" << endl;
}
virtual ~Chinese() {//基类中的析构函数应该使用虚析构
cout << "deconstructing Chinese" << endl;
}
};
class Japanese :virtual public Name {
public:
int passport;
public:
Japanese(const string& name, int passport) : Name(name), passport(passport) {
cout << "constructing Japanese" << endl;
}
virtual ~Japanese() {//基类中的析构函数应该使用虚析构
cout << "deconstructing Japanese" << endl;
}
};
class Baby :public Japanese, public Chinese {
public:
Baby(const string& name1, int id, const string& name2, int passport) : \
Name(name1), Japanese(name2, passport), Chinese(name1, id) {
cout << "constructing Baby" << endl;
}
virtual ~Baby() {
cout << "deconstructing Baby" << endl;
}
};
class B1 {
int x;
public:
B1(int x) : x(x) {
cout << "constructing B1" << endl;
}
virtual ~B1() {
cout << "deconstructing B1" << endl;
}
};
class B2 {
int y;
public:
B2(int y) : y(y) {
cout << "constructing B2" << endl;
}
virtual ~B2() {
cout << "deconstructing B2" << endl;
}
};
class D : public B1, virtual public B2 {
public:
D(int x, int y) : B1(x), B2(y) {
cout << "constructing D" << endl;
}
virtual ~D() {
cout << "deconstructing D" << endl;
}
};
int main() {
cout << "首先验证单继承的基类与派生类的构造函数的执行顺序,\n和初始化列表对构造顺序的影响" << endl<<endl;
Derived d1(6, 7, -2);
cout << "可以看出先执行基类的构造函数,再执行派生类的构造函数\n析构函数相反" << endl << endl;
cout << "接下来验证类组合中的数据成员的函数的执行顺序,\n和初始化列表对构造顺序的影响" << endl << endl;
Car c1(9, -2);
cout << "可以看出来先执行 先声明的数据成员的构造函数" << endl << endl;
cout << "然后验证多继承情况下构造函数的顺序" << endl << endl;
Baby b1("qaq", 2021, "emm", -18);
D d2(0, -3);
cout << "可以看出,先执行先继承的基类的构造函数(如果基类都是普通基类,而非虚基类)\n"\
"若有虚基类则优先执行虚基类的构造函数" << endl << endl;
cout << "最后验证一个基类是普通基类,另一个基类是虚基类的情况" << endl << endl;
D d3(0, -3);
cout << "可以看出优先执行虚基类的构造函数,然后再执行普通基类的虚构函数" << endl << endl;
return 0;
}
12-24
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交