![]() |
#2
lintaoyn2009-08-23 17:11
|

#include <iostream>
using namespace std;
class base
{
public:
base(int i)
{
x=i;
cout<<"constructor of base."<<endl;
}
~base(){cout<<"destructor of base."<<endl;}
viod show(){cout<<"x="<<x<<endl;}
private:
int x;
};
class derived:public base
{
public:
derived(int i):base(i),d(i){cout<<"constructor of derived."<<endl;}
private:
base d;
};
void main()
{
derived obj(5);
obj.show();
}
using namespace std;
class base
{
public:
base(int i)
{
x=i;
cout<<"constructor of base."<<endl;
}
~base(){cout<<"destructor of base."<<endl;}
viod show(){cout<<"x="<<x<<endl;}
private:
int x;
};
class derived:public base
{
public:
derived(int i):base(i),d(i){cout<<"constructor of derived."<<endl;}
private:
base d;
};
void main()
{
derived obj(5);
obj.show();
}
运行结果:
constructor of base.
constructor of base.//怎么有两遍?不懂
constructor of derived.
x=5
destructor of base.
destructor of base.