版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第16周面向?qū)ο蛏蠙C(jī)實(shí)驗(yàn)練習(xí)題類和對象已知一個(gè)名為Complex的復(fù)數(shù)類,這個(gè)類包含:(1)私有成員:實(shí)部、虛部,且均為int型(2)公有的帶默認(rèn)形參值的構(gòu)造函數(shù)、復(fù)制構(gòu)造函數(shù)(3)公有成員函數(shù)Display,其作用為顯示復(fù)數(shù)要求:(1)實(shí)現(xiàn)滿足上述屬性和行為的Complex類定義;(2)設(shè)計(jì)函數(shù)AddComplex,函數(shù)AddComplex功能為實(shí)現(xiàn)兩個(gè)復(fù)數(shù)相加,要求該函數(shù)的形參為復(fù)數(shù)類的常引用;(3)保證如下主函數(shù)能正確運(yùn)行,intmain(){Complexc0(2,-3);Complexc1(c0);cout<<"c1is:";c1.Display();Complexc2(3,4);cout<<"c2is:";c2.Display();Complexc3;c3=AddComplex(c1,c2);cout<<"c3is:";c3.Display(); return0;}且輸出結(jié)果如下:c1is:2-3ic2is:3+4ic3is:5+1i已知表示點(diǎn)的類CPoint和表示線段的CLine類,類CPoint包含:(1)表達(dá)點(diǎn)位置的私有數(shù)據(jù)成員x,y(2)構(gòu)造函數(shù)及復(fù)制構(gòu)造函數(shù)類CLine包含:(1)兩個(gè)CPoint的點(diǎn)對象(該兩點(diǎn)分別為線段的兩個(gè)端點(diǎn))(2)構(gòu)造函數(shù)(提示:構(gòu)造函數(shù)中用初始化列表對內(nèi)嵌對象進(jìn)行初始化)(3)公有成員函數(shù)GetLen,其功能為返回線段的長度(4)類屬性成員count用于記錄創(chuàng)建的CLine類對象的個(gè)數(shù),及用于顯示count值的ShowCount函數(shù);要求:(1)實(shí)現(xiàn)滿足上述屬性和行為的CPoint類及CLine類定義;(2)保證如下主函數(shù)能正確運(yùn)行,#include<iostream>usingnamespacestd;intmain(){ CPointp1(1,1); CPointp2(4,5); CLineline1(p1,p2); cout<<"thelengthofline1is:"<<line1.GetLen()<<endl; CPointp3(0,0); CLineline2(line1); cout<<"Thecountoflineis:"<<CLine::ShowCount()<<endl;return0;}且輸出結(jié)果如下:Thelengthofline1is:5ThecountofLineis:2繼承、派生已知Point類的聲明如下:#include<iostream>usingnamespacestd;//Point類的聲明classPoint {public: //外部接口 Point();//構(gòu)造函數(shù)Point(intxx,intyy);//構(gòu)造函數(shù) Point(Point&p); //拷貝構(gòu)造函數(shù) intGetX()const{returnX;} intGetY()const{returnY;}voidmove(intnewx,intnewy){x=newx;y=newy;}~Point(){}private: //私有數(shù)據(jù) intX,Y;};classArrayofPoints{Point&element(intindex){assert(index>=0&&index<size);//size為數(shù)組大小returnpoints[index];}};//計(jì)算任意兩點(diǎn)之間的距離intL1Distance(intx1,inty1,intx2,inty2){return(abs(x1-x2)+abs(y1-y2));}請給出Point類的構(gòu)造函數(shù),并參照下圖創(chuàng)建動(dòng)態(tài)數(shù)組類ArrayofPoints,實(shí)現(xiàn)主函數(shù)。請自行補(bǔ)充需要的頭文件。//主程序intmain(){intcount; cout<<”PleaseenterthecountofPoints”<<endl;cin>>count;ArrayofPointsArrPoints(count);ArrPoints.element(0).move(5,10);ArrPoints.element(1).move(15,20); cout<<”Thedistanceis”<<endl;cout<<L1Distance(ArrPoints.element(0).GetX(),ArrPoints.element(0).GetY(),ArrPoints.element(1).GetX(),ArrPoints.element(1).GetY()<<endl;}參考程序:#include<iostream>#include<math.h>#include<assert.h>usingnamespacestd;classPoint {public: //外部接口 Point();//構(gòu)造函數(shù) Point(intxx,intyy);//構(gòu)造函數(shù) Point(Point&p); //拷貝構(gòu)造函數(shù) intGetX()const{returnx;} intGetY()const{returny;} voidmove(intnewx,intnewy){x=newx;y=newy;} ~Point(){}private: //私有數(shù)據(jù) intx,y;};Point::Point(){ x=0;y=0;}Point::Point(intxx,intyy){ this->x=xx; this->y=yy;}classArrayofPoints{public: ArrayofPoints(intsize){ this->size=size; } ~ArrayofPoints(){ } Point&element(intindex){assert(index>=0&&index<size);//size為數(shù)組大小Pointpoints[index];returnpoints[index]; }private: intsize;};intL1Distance(intx1,inty1,intx2,inty2){return(abs(x1-x2)+abs(y1-y2));}intmain(){ intcount; cout<<"PleaseenterthecountofPoints"<<endl; cin>>count; ArrayofPointsArrPoints(count); ArrPoints.element(0).move(5,10); ArrPoints.element(1).move(15,20); cout<<"Thedistanceis"<<endl; cout<<L1Distance(ArrPoints.element(0).GetX(),ArrPoints.element(0).GetY(),ArrPoints.element(1).GetX(),ArrPoints.element(1).GetY())<<endl; return0;}定義一個(gè)Date類,有整型數(shù)據(jù)成員year,month,day,成員函數(shù)Display用于顯示日期;定義DateTime類,從Date類公有派生而來,在DateTime類中增加數(shù)據(jù)成員hour,minute,second,設(shè)計(jì)一個(gè)成員函數(shù)Display用于顯示日期時(shí)間信息。參考程序:#include<iostream>usingnamespacestd;classData{public: Data(intyear,intmonth,intday){ this->year=year; this->month=year; this->day=day; cout<<"Data構(gòu)造函數(shù)被調(diào)用!"<<endl; } voidDisplay(){ cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl; } intyear,month,day;};classDataTime:publicData{public: DataTime(inta,intb,intc,intd,inte,intf):Data(a,b,c){ this->hour=d; this->minute=e; this->second=f; } voidDisplay(){ cout<<year<<"年"<<month<<"月"<<day<<"日"<<hour<<"時(shí)"<<minute<<"分"<<"秒"<<endl; }private: inthour,minute,second;};intmain(){ //DataA(2016,11,24); //A.Display(); DataTimea(2016,11,24,13,50,59); a.Display(); return0;}如圖所示:設(shè)計(jì)圖示中的4個(gè)類,其中:電腦類(Computrer)具有frequency,os,ram(CPU主頻,操作系統(tǒng),內(nèi)存容量)等屬性,相機(jī)(Camera)具有pixel(像素?cái)?shù),如500萬,800萬等),從電腦和相機(jī)共同派生出智能手機(jī),智能手機(jī)(Smobilephone)具有size(屏幕尺寸,如4.5寸,5.5寸等)。家電(Appliance)擁有brand,price(品牌,價(jià)格)等屬性。構(gòu)造智能手機(jī)類的一個(gè)實(shí)例,并通過該實(shí)例調(diào)用其成員show()顯示其繼承自基類和自身的所有屬性信息。參考程序:#include<iostream>usingnamespacestd;classAppliance{public: Appliance(charc,floatd){ this->brand=c; this->price=d; } ~Appliance(){ } charGetbrand(){returnbrand;} floatGetprice(){returnprice;}private: charbrand; floatprice;};classComputer:virtualpublicAppliance{public: Computer(floata,charb,intc,chard,floate):Appliance(d,e){ this->frequency=a; this->os=b; this->ran=c; } ~Computer(){ } floatGetfrequency(){ returnfrequency; } charGetos(){ returnos; } intGetran(){ returnran; }private: floatfrequency; charos; intran; };classCamera:virtualAppliance{public: Camera(inta,chard,floate):Appliance(d,e){ this->pixel=a; } intGetpixel(){ returnpixel; }private: intpixel;};classSmobilephone:publicComputer,publicCamera{public: Smobilephone(floata,charb,intc,chard,floate):Appliance(d,e),Computer(a,b,c,d,e),Camera(c,d,e){ this->a=a; } voidshow(){ chara=Getbrand(); cout<<"品牌:"<<a<<endl; floatb=Getprice(); cout<<"價(jià)格:"<<b<<"元"<<endl; floatc=Getfrequency(); cout<<"頻率:"<<c<<"KHz"<<endl; chard=Getos(); cout<<"操作系統(tǒng):"<<d<<endl; inte=Getran(); cout<<"內(nèi)存:"<<e<<endl; intf=Getpixel(); cout<<"像素:"<<f<<"萬"<<endl; } private: floata;};intmain(){ Smobilephonea(2.5,'a',5,'e',3.59); a.show(); return0;}運(yùn)算符重載多態(tài)模板已知一個(gè)名為Complex的復(fù)數(shù)類,這個(gè)類包含:(1)私有成員:實(shí)部、虛部,且均為int型(2)公有的帶默認(rèn)形參值的構(gòu)造函數(shù)(3)以類的成員函數(shù)方式重載+運(yùn)算符和*運(yùn)算符(4)以友元函數(shù)方式重載<<運(yùn)算符(5)使得以下主函數(shù)正確運(yùn)行并得到以下結(jié)果intmain(){Complexc1(1,2);Complexc2(3,4);
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 《落花生》-第二課時(shí)教學(xué)設(shè)計(jì)
- 2024混凝土供貨合同樣本
- 2024大學(xué)場地租賃合同范本
- 玻璃淋浴房維修合同
- 2025考研輔導(dǎo)培訓(xùn)課件
- 四川省合江縣馬街中學(xué)2024-2025學(xué)年高三上學(xué)期9月月考物理試題答案
- 美容美發(fā)業(yè)數(shù)字化門店管理解決方案
- 期末Module3Module4復(fù)習(xí)提綱七年級英語下冊
- 臺風(fēng)預(yù)警工作匯報(bào)
- 銀行貸款融資工作總結(jié)匯報(bào)
- 九年級化學(xué)上冊(滬教版2024)新教材解讀課件
- JGT503-2016承插型盤扣式鋼管支架構(gòu)件
- 2024-2030年中國橙子市場調(diào)研及發(fā)展策略研究報(bào)告
- 2024-2030年中國精神病醫(yī)院行業(yè)發(fā)展分析及發(fā)展前景與趨勢預(yù)測研究報(bào)告
- 行政職業(yè)能力測試-數(shù)量關(guān)系真題附參考答案(達(dá)標(biāo)題)
- (高清版)JTG 2111-2019 小交通量農(nóng)村公路工程技術(shù)標(biāo)準(zhǔn)
- 2023年上海市浦東新區(qū)中考二模語文試題(解析版)
- 2024年4月自考00023高等數(shù)學(xué)工本試題及答案含評分參考
- 中醫(yī)養(yǎng)生保健-秋季養(yǎng)生
- 三甲醫(yī)院智能化排隊(duì)叫號與信息發(fā)布系統(tǒng)設(shè)計(jì)方案
- 《果蔬貯藏與加工技術(shù)》課件-項(xiàng)目一 果蔬貯藏保鮮基本技術(shù)
評論
0/150
提交評論