继承的运行结果
继承的运行结果#include <iostream>
using namespace std;
class data
{
public:
data(int x)
{
data::x=x;
cout<<"data cons."<<endl;
}
~data(){cout<<"data des."<<endl;}
private:
int x;
};
class base
{
public:
base(int x):d1(x){cout<<"base cons."<<endl;}
~base(){cout<<"base des"<<endl;}
private:
data d1;
};
class derived:public base
{
public:
derived(int x):base(x),d2(x){cout<<"derived cons."<<endl;}
~derived(){cout<<"derived des."<<endl;}
private:
data d2;
};
void main()
{
derived obj(5);
}
结果是这样的:
data cons.//搞不懂怎么会有这行结果
base cons.
data cons.
derived cons.
derived des.
data des.
base des.
data des.
我在书上看到这句话:对于派生类的构造函数,在定义对象时构造函数的执行顺序:先执行基类的构造函数,在执行子对象的构造函数,最后执行派生类本身的构造函数。我就搞不懂子对象的构造函数指的是什么呢?能不能用上面那个例子解释一下??
[ 本帖最后由 jjg 于 2009-8-26 10:29 编辑 ]