阅读代码:
#include <iostream>
using namespace std;
class A
{
protected:
int x;
public:
A(int x)
{
A::x=x;
cout<<"class A"<<endl;
}
};
class B
{
private:
A a1;
public:
B(int x):a1(x)
{
cout<<"class B"<<endl;
}
};
class C:public B
{
private:
A a2;
public:
C(int x):B(x),a2(x)
{
cout<<"class C"<<endl;
}
};
class D:public C
{
public:
D(int x):C(x)
{
cout<<"class D"<<endl;
}
};
int main()
{
D dobj(10);
return 0;
}
预计运行结果:
class A
class B
class A
class C
class D
实际运行结果: