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

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

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

2019-11-09 21:02:32
字體:
供稿:網(wǎng)友
#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;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 井冈山市| 清原| 云安县| 阳新县| 奉节县| 诏安县| 陈巴尔虎旗| 秦安县| 宜阳县| 沅陵县| 哈尔滨市| 淅川县| 乐陵市| 靖江市| 平谷区| 双峰县| 即墨市| 安塞县| 钟祥市| 洛浦县| 张家界市| 精河县| 枞阳县| 治多县| 芦山县| 昌江| 通州市| 新余市| 永平县| 连州市| 赣州市| 洪洞县| 历史| 庄浪县| 周口市| 吴桥县| 长治市| 张家口市| 穆棱市| 穆棱市| 玉屏|