//多重继承 #include <iostream> using namespace std; class A { public: int a; A(int a=0):a(a) { cout<<"A基类A::A()"<<endl; } ~A() { cout<<"A基类A::~A()"<<endl; } void show() { cout<<"A基类A::a="<<a<<endl; } }; class B:public A { protected: int b; public: B(int a=0,int b=0):A(a),b(b) { cout<<"B子类B:B()"<<endl; } ~B() { cout<<"B子类B:~B()"<<endl; } void show() { cout<<"B子类B::b="<<b<<endl; } }; class C:public A { protected: int c; public: C(int a=0,int c=0):A(a),c(c) { cout<<"C子类C::C()"<<endl; } ~C() { cout<<"C子类C::~C()"<<endl; } void show() { cout<<"C子类C::c="<<c<<endl; } }; class D:public B,public C { protected: int d; public: D(int a=0,int b=0,int c=0,int d=0):d(d),B(a,b),C(a,c) { cout<<"D孙子类D::D()"<<endl; } ~D() { cout<<"D孙子类D::~D()"<<endl; } void show() { cout<<"D孙子类D::c="<<d<<endl; } }; void Display(A *p) { p->show(); } int main(int argc, char *argv[]) { A a(1); B b(2,3); C c(4,5); D d(6,7,8,9); A *p; p=&a; p->show(); p=&b; p->show(); p=&c; p->show(); B *q; q=&d; q->show(); return 0; }
时间: 2024-10-22 19:43:10