#include<iostream>
using namespace std;
class A
{
public:
A(int x = 0 ):a(x)
{
cout << "A " << a << endl;
}
private:
int a;
};
class B:public A
{
public:
B(int v = 0):c(v++),x(v++),y(v++),d(v++),A(v++)
{
//先执行初始化列表:1.先初始化基类;2.按照定义顺序依次初始化
//再执行初始化函数中的类容
//构造函数结束,等价派生类构造完成
b = v++;
cout << "b " << b << endl;
cout << "c " << c << endl;
cout << "d " << d << endl;
cout << "B " << v << endl;
}
/*
A 0
A 3
A 4
b 5
c 1
d 2
B 6
*/
private:
int b, c;
const int d;
A x, y;
};
int main()
{
B b(0);
return 0;
}
#include<iostream>
using namespace std;
class A
{
public:
A(int x = 0 ):a(x)
{
/*cout << "构造A " << a << endl;*/
}
~A()
{
cout << "析构A" << a << endl;
}
private:
int a;
};
class B:public A
{
public:
B(int v = 0):c(v++),x(v++),y(v++),d(v++),A(v++)
{
//先执行初始化列表:1.先初始化基类;2.按照定义顺序依次初始化
//再执行初始化函数中的类容
//构造函数结束,等价派生类构造完成
b = v++;
/* cout << "b " << b << endl;
cout << "c " << c << endl;
cout << "d " << d << endl;
cout << "B " << v << endl;*/
}
~B()
{
cout << "析构B" << endl;
}
private:
int b, c;
const int d;
A x, y;
};
int main()
{
B b(0);
return 0;
}
/*
* 1.先析构派生类,2.析构派生类对象成员3.析构基类
*这是构造的一个逆过程
*/