您好,登錄后才能下訂單哦!
//多重繼承 #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:virtual 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:virtual 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; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。