国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++——繼承與組合例子代碼(模擬計算機的組成)

2019-11-09 20:08:32
字體:
來源:轉載
供稿:網友
#include <iostream>#include <string.h>using namespace std;class Hard{PRivate:    char hname[20];public:    Hard() {};    Hard(char *n)    {        strcpy(hname,n);    };    Hard(const Hard& h)    {        strcpy(hname, h.hname);    }    void Show()    {        cout<<"Hard:"<<hname<<" ";    }};class Soft{private:    char sname[20];public:    Soft() {};    Soft(char *n)    {        strcpy(sname,n);    };    Soft(const Soft& s)    {        strcpy(sname, s.sname);    }    void Show()    {        cout<<"Soft:"<<sname<<" ";    }};class Computer:public Hard,public Soft{private:    char  cname[20];public:    Computer() {};    Computer(char * cn,Hard& h, Soft& s):Hard(h),Soft(s)    {        strcpy(cname,cn);    };    Computer(char * cn,char * hn, char * sn):Hard(hn),Soft(sn)    {        strcpy(cname,cn);    };    void Show()    {        cout<<"Computer:"<<cname<<" ";        Hard::Show();        Soft::Show();        cout<<endl;    }};class Computer2{private:    char  cname[20];    Hard hard;    Soft soft;public:    Computer2() {};    Computer2(char * cn,Hard& h, Soft& s):hard(h),soft(s)    {        strcpy(cname,cn);    };    Computer2(char * cn,char * hn, char * sn):hard(hn),soft(sn)    {        strcpy(cname,cn);    };    void Show()    {        cout<<"Computer2:"<<cname<<" ";        hard.Show();        soft.Show();        cout<<endl;    }};int main(){    Hard h("cpu");    Soft s("Windows xp");//繼承    Computer c1("leno",h,s);    Computer c2("leno","cpu","Windows 7");    c1.Show();    c2.Show();//組合    Computer2 c3("leno",h,s);    Computer2 c4("leno","cpu","windows 7");    c3.Show();    c4.Show();    return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 高雄市| 东城区| 同仁县| 西畴县| 微山县| 恩平市| 邵东县| 大石桥市| 黑龙江省| 清水县| 新干县| 祁门县| 仁寿县| 微山县| 古交市| 尉犁县| 安塞县| 湘阴县| 乌鲁木齐县| 天全县| 商水县| 蒲江县| 手游| 松阳县| 聊城市| 吴堡县| 汶川县| 临桂县| 晋宁县| 香河县| 乌什县| 彰化市| 福贡县| 遂宁市| 内乡县| 雷波县| 新龙县| 溧阳市| 都兰县| 旌德县| 连城县|