#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; }
运行结果:
知识点总结:
D是C的派生类 C是B的派生类 A是B、C的子类
时间: 2024-10-27 06:30:01