版權(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)?zāi)康模?1) 理解編譯時(shí)的多態(tài)性和運(yùn)行時(shí)的多態(tài)性。 2) 掌握運(yùn)算符重載的基本方法。 3) 掌握虛函數(shù)的定義與使用方法。二、實(shí)驗(yàn)內(nèi)容及方法:1)定義一個(gè)復(fù)數(shù)類Complex,并重載運(yùn)算符+、-、*、/和負(fù)號(hào)(用友元函數(shù)實(shí)現(xiàn))2) 定義一個(gè)有理數(shù)類,并重載運(yùn)算符+、-、*、/(用成員函數(shù)實(shí)現(xiàn))。 class Rational public: /構(gòu)造函數(shù) Rational(int num1=0,int den1=1); /重載運(yùn)算符 const operator+(const Rational r); const operator-( const Rational r); const o
2、perator*( const Rational r); const operator/( const Rational r); private: /定義一個(gè)有理數(shù)num為分子,den為分母 int num, den; ; 3) 定義一個(gè)類Animal,該類中可以存放動(dòng)物的名字,并有一個(gè)Identify函數(shù)用于顯示動(dòng)物的種類。 4) 定義兩個(gè)類Cat、Dog,都繼承自Animal,并重載Identify函數(shù),不但要顯示動(dòng)物的種類,還要顯示動(dòng)物的名字。 5) 定義一個(gè)Tiger類,繼承自Cat,重載Identify函數(shù),顯示動(dòng)物的種類和名字。 三、實(shí)驗(yàn)程序如下:1、#include<ios
3、tream.h>class complexpublic:complex() real=imag=0;complex(double r) real=r;imag=0.0;complex(double r,double i)real=r;imag=i;friend complex operator+(complex,complex); friend complex operator-(complex,complex);friend complex operator*(complex,complex);friend complex operator/(complex,complex);frie
4、nd complex operator-(complex &); void Display();private:double real,imag;complex operator+(complex c1,complex c2)complex t;t.real=c1.real+c2.real;t.imag=c1.imag+c2.imag;return t;complex operator-(complex c1,complex c2)complex t;t.real=c1.real-c2.real;t.imag=c1.imag-c2.imag;return t; complex oper
5、ator*(complex c1,complex c2)complex t;t.real=c1.real*c2.real-c1.imag*c2.imag;t.imag=c1.real*c2.imag+c1.imag*c2.real;return t; complex operator/(complex c1,complex c2)complex t; t.real=(c1.real*c2.real+c1.imag*c2.imag)/(c1.real*c1.real+c1.imag*c2.imag);t.imag=(c2.real*c1.imag-c1.real*c2.imag)/(c1.rea
6、l*c1.real+c1.imag*c2.imag);return t;complex operator-(complex &c)complex t;t.real=-c.real;t.imag=-c.imag;return t;void complex:Display()cout<<real;if(imag>0)cout<<"+"if(imag!=0)cout<<"i"<<endl;void main()complex c1(5.0,2.4),c2(3.0,-2.5),c3;c3=c1+c2;
7、cout<<"c3="c3.Display();c3=c1-c2;cout<<"c3="c3.Display();c3=c1*c2;cout<<"c3="c3.Display();c3=c1/c2;cout<<"c3="c3.Display();c3=-c1;cout<<"c3="c3.Display();2、#include <iostream.h>#include <math.h>class Rational
8、public: /構(gòu)造函數(shù) Rational(int num1=0,int den1=1); /重載運(yùn)算符 const Rational operator+(const Rational &r); const Rational operator-( const Rational &r); const Rational operator*( const Rational &r); const Rational operator/( const Rational &r); /重載輸出符 friend ostream& operator <<(os
9、tream& outs,const Rational &r); private: /定義一個(gè)有理數(shù)num為分子,den為分母 int num, den; void Reduction (); /約分;Rational:Rational(int num1/* =0 */,int den1/* =1 */) num = num1; den = den1;const Rational Rational:operator+(const Rational& r) Rational temp; temp.den = this->den * r.den; temp.num =
10、this->num * r.den + this->den * r.num; temp.Reduction(); return temp;const Rational Rational:operator-(const Rational& r) Rational temp; temp.den = this->den * r.den; temp.num = this->num * r.den - this->den * r.num; temp.Reduction(); return temp;const Rational Rational:operator*(
11、const Rational& r) Rational temp; temp.den = this->den * r.den; temp.num = this->num * r.num; temp.Reduction(); return temp;const Rational Rational:operator/(const Rational& r) Rational temp; temp.den = this->den * r.num; temp.num = this->num * r.den; temp.Reduction(); return tem
12、p;void Rational:Reduction() int i,z; if (den=0) cout<<"data error"<<endl; z=den>num?den:num; for(i=2;i<=sqrt(z);i+) while(den%i=0 && num%i=0)/here den=den/i; num=num/i; ostream& operator<<(ostream& outs, const Rational& r) outs<<r.num<<"/"<<r.den<<endl; return outs;int main() Rational r1(1,2),r2(1,3); cout<<"r1:"<<r1; cout<<"r2:"<
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年內(nèi)容營(yíng)銷與房地產(chǎn)推廣的結(jié)合
- 2026年家居用品銷售(需求分析)試題及答案
- 2025年中職廣播電視學(xué)(廣播電視基礎(chǔ))試題及答案
- 2026年扁平化管理與商務(wù)運(yùn)營(yíng)的成功案例
- 2025年大學(xué)計(jì)算機(jī)應(yīng)用技術(shù)(網(wǎng)絡(luò)系統(tǒng)基礎(chǔ))試題及答案
- 2026年幼兒教育(幼兒藝術(shù)教育)試題及答案
- 2025年中職第三學(xué)年(制冷和空調(diào)設(shè)備運(yùn)行與維修)制冷設(shè)備安裝工藝試題及答案
- 2025年中職(會(huì)計(jì)電算化)報(bào)表分析綜合技能測(cè)試試題及答案
- 2026年物流調(diào)度(應(yīng)急處理)試題及答案
- 2025年中職(藥學(xué)類)藥學(xué)綜合實(shí)務(wù)綜合測(cè)試試題及答案
- 2026年榆能集團(tuán)陜西精益化工有限公司招聘?jìng)淇碱}庫(kù)完整答案詳解
- 2026廣東省環(huán)境科學(xué)研究院招聘專業(yè)技術(shù)人員16人筆試參考題庫(kù)及答案解析
- 邊坡支護(hù)安全監(jiān)理實(shí)施細(xì)則范文(3篇)
- 6.1.3化學(xué)反應(yīng)速率與反應(yīng)限度(第3課時(shí) 化學(xué)反應(yīng)的限度) 課件 高中化學(xué)新蘇教版必修第二冊(cè)(2022-2023學(xué)年)
- 北京市西城區(qū)第8中學(xué)2026屆生物高二上期末學(xué)業(yè)質(zhì)量監(jiān)測(cè)模擬試題含解析
- 2026年遼寧輕工職業(yè)學(xué)院?jiǎn)握芯C合素質(zhì)考試參考題庫(kù)帶答案解析
- 2026屆北京市清華大學(xué)附中數(shù)學(xué)高二上期末調(diào)研模擬試題含解析
- 醫(yī)院實(shí)習(xí)生安全培訓(xùn)課課件
- 四川省成都市武侯區(qū)西川中學(xué)2024-2025學(xué)年八上期末數(shù)學(xué)試卷(解析版)
- 2026年《必背60題》抖音本地生活BD經(jīng)理高頻面試題包含詳細(xì)解答
- 《成人患者醫(yī)用粘膠相關(guān)性皮膚損傷的預(yù)防及護(hù)理》團(tuán)體標(biāo)準(zhǔn)解讀2026
評(píng)論
0/150
提交評(píng)論