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

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

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

2019-11-09 20:52:41
字體:
來(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)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 阳谷县| 兴山县| 兖州市| 安阳市| 麟游县| 钟山县| 吉隆县| 娄烦县| 长丰县| 南丰县| 枣阳市| 通州市| 黑山县| 荥阳市| 长武县| 江口县| 马龙县| 甘德县| 古丈县| 翁源县| 东台市| 堆龙德庆县| 会同县| 喀喇沁旗| 绍兴县| 合阳县| 沙雅县| 菏泽市| 永和县| 姜堰市| 横峰县| 如皋市| 布尔津县| 大港区| 庄浪县| 三穗县| 湟中县| 漳浦县| 巴彦淖尔市| 会昌县| 河池市|