[c++]多繼承

來源:互聯網
上載者:User

標籤:c++   派生   類   繼承   

多繼承格式:

class 類名:繼承方式1 基類1,繼承方式2 基類2,........

#include<iostream>using namespace std;class X{    int x;public:    X(int a = 0)    {x = a;cout<<"constructing X "<<x<<endl;}    void set_x(int a)    {x = a;}    void show_x()    {cout<<"x = "<<x<<endl;}    ~X()    {cout<<"destructing X "<<x<<endl;}};class Y{    int y;public:    Y(int a = 0)    {y = a;cout<<"constructing Y "<<y<<endl;}    void set_y(int b)    {y = b;}    void show_y()    {cout<<"y = "<<y<<endl;;}    ~Y()    {cout<<"destructing Y "<<y<<endl;}};class Z:public X,public Y//多繼承{    int z;public:    Z(int a = 0,int b = 0,int c = 0):X(a),Y(b)    {        z = c;        cout<<"constructing Z "<<z<<endl;    }    void set_xyz(int a,int b,int c)    {        set_x(a);        set_y(b);        z = c;    }    void show_z()    {        cout<<"z = "<<z<<endl;    }    void show()    {        show_x();        show_y();        cout<<"z = "<<z<<endl;//類內可直接用    }    ~Z()    {cout<<"destructing Z "<<z<<endl;}};int main(){    Z obj1(3,4,5);    Z obj2;    obj2.set_xyz(10,20,30);    obj1.show_x();    obj1.show_y();    obj1.show_z();    obj2.show();    return 0;}


[c++]多繼承

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.