實(shí)驗(yàn)5多態(tài)性和運(yùn)算符重載【保證能編譯連接無(wú)措】_第1頁(yè)
實(shí)驗(yàn)5多態(tài)性和運(yùn)算符重載【保證能編譯連接無(wú)措】_第2頁(yè)
實(shí)驗(yàn)5多態(tài)性和運(yùn)算符重載【保證能編譯連接無(wú)措】_第3頁(yè)
實(shí)驗(yàn)5多態(tài)性和運(yùn)算符重載【保證能編譯連接無(wú)措】_第4頁(yè)
實(shí)驗(yàn)5多態(tài)性和運(yùn)算符重載【保證能編譯連接無(wú)措】_第5頁(yè)
已閱讀5頁(yè),還剩5頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、實(shí)驗(yàn)5 多態(tài)性和運(yùn)算符重載一、 姓名 班級(jí) 電信13F 學(xué)號(hào) 二、 實(shí)驗(yàn)?zāi)康耐ㄟ^(guò)vc開(kāi)發(fā)環(huán)境下派生類多態(tài)性的設(shè)計(jì)與實(shí)現(xiàn),了解運(yùn)行時(shí)多態(tài)的概念,掌握派生類多態(tài)性基本設(shè)計(jì)和編程方法。三、 實(shí)驗(yàn)主要內(nèi)容1、 有三角形、正方形和圓形三種圖形,求它們各自的面積??上瘸橄蟪鲆粋€(gè)基類,在基類中聲明一個(gè)虛函數(shù),用于求面積,并利用單界面設(shè)計(jì)各個(gè)圖形求面積的方法。2、 使用成員函數(shù)重載實(shí)現(xiàn)復(fù)數(shù)四則運(yùn)算、邏輯運(yùn)算和關(guān)系運(yùn)算;3、 使用友元函數(shù)重載實(shí)現(xiàn)字符串連接運(yùn)算,賦值運(yùn)算;四、 實(shí)驗(yàn)程序1. #include<iostream>#include<math.h>using namespace

2、 std;#define PI 3.14class basepublic:virtual double area()return 0;class triangle:public baseprotected:double h; double w;public:triangle(double H,double W)h=H; w=W;double area()return 0.5*h*w;class square:public baseprotected:double a;public:square(double A)a=A;double area()return a*a;class circle:

3、public baseprotected:double radius;public:circle(double Rad)radius=Rad;double area()return PI * radius * radius;void main()base *a,*b,*c;a=new triangle(2,8);b=new square(9);c=new circle(5);cout<<a->area ()<<endl;cout<<b->area ()<<endl;cout<<c->area ()<<en

4、dl; 2. #include<iostream>using namespace std;class complex /復(fù)數(shù)protected:double real; /實(shí)部 double imag; /虛部public:complex();complex(double Real,double Imag)real=Real; imag=Imag;complex operator+ (complex&); /使用成員函數(shù)重載實(shí)現(xiàn)復(fù)數(shù)加法運(yùn)算complex operator- (complex&); /使用成員函數(shù)重載實(shí)現(xiàn)復(fù)數(shù)減法運(yùn)算complex operator*

5、 (complex&); /使用成員函數(shù)重載實(shí)現(xiàn)復(fù)數(shù)乘法運(yùn)算complex operator/ (complex&); /使用成員函數(shù)重載實(shí)現(xiàn)復(fù)數(shù)除法運(yùn)算 complex operator&&(complex&); /使用成員函數(shù)重載實(shí)現(xiàn)與運(yùn)算complex operator|(complex&); /使用成員函數(shù)重載實(shí)現(xiàn)或運(yùn)算 complex operator! (); /使用成員函數(shù)重載實(shí)現(xiàn)非運(yùn)算 complex operator<(complex&); /使用成員函數(shù)重載實(shí)現(xiàn)或運(yùn)算void print( complex&

6、; c) /輸出結(jié)果if(c.imag<0) cout<<c.real<<c.imag<<'i'<<"n"else cout<<c.real<<'+'<<c.imag<<'i'<<"n" ;complex complex: operator+ (complex& c) /加法return complex(real+c.real,imag+c.imag);complex complex:

7、operator- (complex& c) /減法return complex(real-c.real,imag-c.imag);complex complex: operator* (complex& c) /乘法return complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real); complex complex: operator/ (complex &c) /除法return complex(real * c.real + imag + c.imag) / (c.real *

8、c.real + c.imag * c.imag),(imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag); complex complex: operator&& (complex& c) /與運(yùn)算return complex(real&&c.real,imag&&c.imag);complex complex: operator| (complex& c) /或運(yùn)算return complex(real|c.real,imag|c.imag);

9、complex complex: operator! () /非運(yùn)算return complex(!real,!imag);complex complex: operator< (complex& c) /小于運(yùn)算if(real<c.real) return complex(real,imag);else return complex(c.real,c.imag);void main()complex c1(1.0, -0.0), c2(3.0, -1.0), c3; c3 = c1 + c2; cout<<"c1+c2=" c3.print

10、(c3);c3 = c1 - c2; cout<<"c1-c2=" c3.print(c3);c3 = c1 * c2; cout<<"c1*c2=" c3.print(c3);c3 = c1 / c2; cout<<"c1/c2=" c3.print(c3);c3 = c1&&c2; cout<<"c1&&c2=" c3.print(c3);c3 = c1|c2; cout<<"c1|c2=" c3.p

11、rint(c3);c3=!c1; cout<<"!c1=" c3.print(c3);c3=c1<c2; cout<<"輸出實(shí)部較小的復(fù)數(shù)=" c3.print(c3); 3. #include<iostream>#include<string.h>using namespace std;class employee /員工類protected:char name20; /員工名字char id20; /員工號(hào)double salary; /員工薪水public:employee();employee

12、(char* emp_name,char* emp_id) /建立員工基本信息strcpy(name,emp_name); strcpy(id,emp_id);char* get_name() /獲取名字return name;char* get_id() /獲取職工號(hào)return id;double get_salary() /應(yīng)得薪水return salary;class hourlyworker:public employee /時(shí)薪工類protected:int hours;double hourpay;public:hourlyworker();hourlyworker(char*

13、hour_name,char* hour_id,int hour,double pay):employee(hour_name,hour_id)hours=hour; hourpay=pay;char *get_name()return employee:get_name ();char *id()return employee:get_id ();int get_hours() /獲取工作時(shí)間return hours;double get_hourpay() /獲取小時(shí)工資return hourpay;double get_salary() /時(shí)薪工工資計(jì)算if(hours<=50)s

14、alary=hourpay*hours;elsesalary=hourpay*50+2*hourpay*(hours-50);return salary;class pieceworker:public employee /計(jì)件工類protected:int pieces; /工件數(shù)double piecepay; /每個(gè)工件贏得的工資public:pieceworker();pieceworker(char* piece_name,char* piece_id,int piece,double piece_pay):employee(piece_name,piece_id)pieces=pi

15、ece; piecepay=piece_pay;char *get_name()return employee:get_name ();char *id()return employee:get_id ();int get_pieces() /獲取工件數(shù)return pieces; double get_pay() /每件的工資return piecepay;double piece_salary() /計(jì)件工工資salary=pieces*piecepay;return salary;class manager:public employeepublic:manager(char* man_

16、name,char* man_id):employee(man_name,man_id)char *get_name()return employee:get_name ();char *id()return employee:get_id ();double manager_salary() /經(jīng)理工資return salary=3000; class commissionworker:public employee /傭金工類protected:double basesalary; /保底新書double total; /一周內(nèi)的銷售總額double percent; /提成的額度publ

17、ic:commissionworker();commissionworker(char* com_name,char* com_id,double com_basesalary,double com_total,double com_percent):employee(com_name,com_id)basesalary=com_basesalary; total=com_total; percent=com_percent;char *get_name()return employee:get_name ();char *id()return employee:get_id ();doubl

18、e get_base_pay() /保底薪水return basesalary;double get_com_total() /銷售總額return total;double get_com_percent() /銷售額中提成return percent;double com_salary() /傭金工的工資salary=basesalary+total*percent;return salary;void main() cout<<"輸出時(shí)薪工信息:"<<"n" hourlyworker hworker("may&qu

19、ot;,"1413250",75,20.5); cout<<" hourlyworker'名字: "<<hworker.get_name ()<<"n" cout<<" hourlyworker'職工號(hào): "<<hworker.get_id ()<<"n" cout<<" hourlyworker'工作小時(shí)數(shù):"<<hworker.get_hours ()&

20、lt;<"n" cout<<" hourlyworker'每小時(shí)金額:"<<hworker.get_hourpay ()<<"n" cout<<" hourlyworker'總工資: "<<hworker.get_salary ()<<"n" cout<<"*"<<"n" cout<<"輸出計(jì)件工信息:"&l

21、t;<"n" pieceworker pworker("march","1413258",100,25.5); cout<<" pieceworker'名字: "<<pworker.get_name ()<<"n" cout<<" pieceworker'職工號(hào): "<<pworker.get_id ()<<"n" cout<<" piece

22、worker'工件數(shù): "<<pworker.get_pieces ()<<"n" cout<<" pieceworker'每件的金額:"<<pworker.get_pay ()<<"n" cout<<" pieceworker'總工資: "<<pworker.piece_salary ()<<"n" cout<<"*"<<

23、"n" cout<<"輸出經(jīng)理信息:"<<"n" manager manag("Jack","000003"); cout<<" manager'名字: "<<manag.get_name ()<<"n" cout<<" manager'職工號(hào):"<<manag.get_id ()<<"n" cout<

24、;<" manager'總工資:"<<manag.manager_salary ()<<"n" cout<<"*"<<"n" cout<<"輸出傭金工信息:"<<"n" commissionworker cworker("Jim","1413257",578,254,0.01); cout<<" commissionworker&

25、#39;名字: "<<cworker.get_name ()<<"n" cout<<" commissionworker'職工號(hào): "<<cworker.get_id ()<<"n" cout<<" commissionworker'保底工資:"<<cworker.get_base_pay ()<<"n" cout<<" commissionworker'銷售額: "<<cworker.get_com_total ()<<"n" cout<<" commissionworker'提成額度:&qu

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論