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

首頁(yè) > 編程 > C++ > 正文

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

2019-11-10 18:29:58
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(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ā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 犍为县| 宁城县| 资溪县| 手游| 玉山县| 革吉县| 江阴市| 白沙| 宿州市| 临江市| 家居| 延安市| 夹江县| 左云县| 宣威市| 繁昌县| 元朗区| 绥化市| 纳雍县| 佳木斯市| 镇平县| 景谷| 河东区| 阳曲县| 新余市| 独山县| 贵南县| 秦安县| 西盟| 巩留县| 兴义市| 临泉县| 永济市| 灵川县| 旌德县| 合江县| 宁蒗| 富锦市| 公主岭市| 特克斯县| 西乌珠穆沁旗|