實(shí)驗(yàn)五-運(yùn)算符重載--完成_第1頁(yè)
實(shí)驗(yàn)五-運(yùn)算符重載--完成_第2頁(yè)
實(shí)驗(yàn)五-運(yùn)算符重載--完成_第3頁(yè)
實(shí)驗(yàn)五-運(yùn)算符重載--完成_第4頁(yè)
實(shí)驗(yàn)五-運(yùn)算符重載--完成_第5頁(yè)
已閱讀5頁(yè),還剩11頁(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、精選優(yōu)質(zhì)文檔-傾情為你奉上 實(shí)驗(yàn)五 運(yùn)算符重載的應(yīng)用班級(jí):B135A2學(xué)號(hào): 姓名: 楊弘 成績(jī): 一 實(shí)驗(yàn)?zāi)康?、理解運(yùn)算符重載的作用;2、掌握運(yùn)算符重載的兩種方法;3、掌握單目、雙目運(yùn)算符的重載;二 使用的設(shè)備和儀器計(jì)算機(jī)+Windows XP +Visual C+6.0三 實(shí)驗(yàn)內(nèi)容及要求1、定義一個(gè)復(fù)數(shù)類(lèi)Complex,重載運(yùn)算符“-”,使之能用于復(fù)數(shù)與實(shí)數(shù)的減法運(yùn)算。參加運(yùn)算的兩個(gè)操作數(shù)一個(gè)是類(lèi)的對(duì)象,一個(gè)是實(shí)數(shù),順序任意。例如:i-c,c-i均合法(其中,c為復(fù)數(shù)類(lèi)的對(duì)象,i為實(shí)數(shù))。減法規(guī)則:復(fù)數(shù)實(shí)部與實(shí)數(shù)相減,復(fù)數(shù)虛部不變。2、定義點(diǎn)類(lèi)Point,重載運(yùn)算符“”、“”、“”、“!

2、=”、“+”、“-”、“>>”、“<<”,實(shí)現(xiàn)兩個(gè)點(diǎn)的相加、相減、相等、不等、自增、自減、輸入和輸出運(yùn)算。3、定義一個(gè)矩陣類(lèi)Matrix,均為M行N列,通過(guò)重載運(yùn)算符“+”、“-”,“<<”,“>>”,“+”,“-”,“=”,“!=”來(lái)實(shí)現(xiàn)矩陣的相加、相減、輸出、輸入、自增、自減以及相等、不等的判斷。4、定義時(shí)間類(lèi)Time,時(shí)間的表示采用24小時(shí)制。重載運(yùn)算符“<<”和“>>”實(shí)現(xiàn)時(shí)間的輸出和輸入;重載運(yùn)算符“+”和“-”實(shí)現(xiàn)時(shí)間推后和提前若干分鐘;重載運(yùn)算符“+”和“-”實(shí)現(xiàn)當(dāng)前時(shí)間推后和提前1小時(shí);重載“>”、“

3、<”、“=”來(lái)判斷兩個(gè)時(shí)間之間大于、小于、等于以及不等于的關(guān)系。注意:輸入時(shí)需對(duì)數(shù)據(jù)合法性進(jìn)行判斷。5、編寫(xiě)一個(gè)程序,處理一個(gè)復(fù)數(shù)與一個(gè)double型數(shù)相加的運(yùn)算,結(jié)果存放在一個(gè)double型的變量d1中,輸出d1的值,再以復(fù)數(shù)形式輸出此值。定義復(fù)數(shù)(Complex)類(lèi),在成員函數(shù)中包含重載類(lèi)型轉(zhuǎn)換運(yùn)算符:Operator double() return real;6、定義一個(gè)教師類(lèi)和一個(gè)學(xué)生類(lèi),二者有一部分?jǐn)?shù)據(jù)成員是相同的,例如num,name,sex。請(qǐng)編程,將一個(gè)學(xué)生類(lèi)對(duì)象轉(zhuǎn)換為教師類(lèi),只將以上3個(gè)相同的數(shù)據(jù)成員移植過(guò)去??梢栽O(shè)想為:一位學(xué)生大學(xué)畢業(yè)了,留校擔(dān)任教師,他原有的部分?jǐn)?shù)

4、據(jù)對(duì)現(xiàn)在的教師身份來(lái)說(shuō)仍然是有用的,應(yīng)當(dāng)保留并成為其教師的數(shù)據(jù)的一部分。四 實(shí)驗(yàn)步驟1.程序代碼:/*1、定義一個(gè)復(fù)數(shù)類(lèi)Complex,重載運(yùn)算符"-",使之能用于復(fù)數(shù)與實(shí)數(shù)的減法運(yùn)算。參加運(yùn)算的兩個(gè)操作數(shù)一個(gè)是類(lèi)的對(duì)象,一個(gè)是實(shí)數(shù),順序任意。例如:i-c,c-i均合法(其中,c為復(fù)數(shù)類(lèi)的對(duì)象,i為實(shí)數(shù))。減法規(guī)則:復(fù)數(shù)實(shí)部與實(shí)數(shù)相減,復(fù)數(shù)虛部不變。*/#include<iostream> /#include<iostream.h>using std:cin;using std:cout;using std:endl;/using namespace

5、 std;class Complex /定義復(fù)數(shù)類(lèi)private:double real,imag;public:Complex()real=0;imag=0;Complex(double a,double b)real=a;imag=b;void Display();void Input();friend Complex operator-(Complex &a,double b);friend Complex operator-(double b,Complex &a);Complex operator-(Complex &a,double b) /定義“-”重載函

6、數(shù)return Complex(a.real-=b,a.imag);Complex operator-(double b,Complex &a)return Complex(a.real-=b,a.imag);void Complex:Input()cout<<"*輸入*"<<endl;cout<<"請(qǐng)輸入復(fù)數(shù)的虛部和實(shí)部:"cin>>real>>imag;void Complex:Display()cout<<"*顯示*"<<endl; co

7、ut<<real<<"+"<<imag<<"i"<<endl;int main() /主函數(shù)Complex c(1,1),c1;double m;c.Display();cout<<"請(qǐng)輸入需要與復(fù)數(shù)相減的實(shí)數(shù):"cin>>m; c=c-m;c.Display();return 0;運(yùn)行結(jié)果:2.程序代碼:/*2、定義點(diǎn)類(lèi)Point,重載運(yùn)算符""、""、""、"!="、&

8、quot;+"、"-"、">>"、"<<",實(shí)現(xiàn)兩個(gè)點(diǎn)的相加、相減、相等、不等、自增、自減、輸入和輸出運(yùn)算。*/#include<iostream> #include<iostream.h>/using std:cin; /用#include<iostream>加上Using std:cin;using / std:cout;using std:endl;對(duì)重載流插入、流提取運(yùn)算符錯(cuò)誤/using std:cout;/using std:endl;/using na

9、mespace std;class Point /定義point類(lèi)private:double x;double y;static int count;public:Point()x=0;y=0;void Input();void Display(); Point operator+(Point &b); Point operator-(Point &b);friend int operator=(Point &a,Point &b);friend int operator!=(Point &a,Point &b);friend Point op

10、erator+(Point &a);friend Point operator+(Point &a,int);friend Point operator-(Point &a); friend Point operator-(Point &a,int);friend istream & operator>>(istream & input,Point & a); friend ostream & operator<<(ostream & output,Point & a);int Point:

11、count=0; /注意賦值時(shí)沒(méi)有static," int static Point:count=0; " wrong!void Point:Input()cout<<"*輸入*"<<endl;cout<<"請(qǐng)輸入點(diǎn)坐標(biāo)(x,y)"cin>>x>>y;count+;void Point:Display()cout<<"*顯示*"<<endl;cout<<"第"<<count<<

12、"個(gè)坐標(biāo)為:"<<"("<<x<<","<<y<<")"<<endl;Point Point:operator+(Point &b) /重載“+”為成員函數(shù)Point p;p.x=x+b.x;p.y=y+b.y;return p;Point Point:operator-(Point &b) /重載“-”為成員函數(shù)Point p;p.x=x-b.x;p.y=y-b.y;return p;int operator=(Point &

13、amp;a,Point &b) /重載“=”為友員函數(shù)if(a.x!=b.x)return 0;else if(a.y!=b.y)return 0;elsereturn 1;int operator!=(Point &a,Point &b) /重載“!=”為友員函數(shù)if(a.x=b.x&&a.y=b.y)return 0;else return 1;Point operator+(Point &a) /重載“+”為友員函數(shù)Point p;p.x=+a.x;p.y=+a.y;return p;Point operator+(Point &a

14、,int) /重載“+”為友員函數(shù)Point c;c.x=a.x;c.y=a.y;a.x+;a.y+;return c; Point operator-(Point &a) /重載“-”為友員函數(shù)Point p;p.x=-a.x;p.y=-a.y;return p;Point operator-(Point &a,int) /重載“-”為友員函數(shù)Point c;c.x=a.x;c.y=a.y;a.x-;a.y-;return c; istream & operator>>(istream & input,Point & a) /重載流插入運(yùn)算

15、符input>>a.x>>a.y;return input;ostream & operator<<(ostream & output,Point & a) /重載流插入運(yùn)算符output<<"("<<a.x<<","<<a.y<<")"return output;int main() /主函數(shù)Point a,b;a.Input();a.Display();b.Input();b.Display();a=a+b;co

16、ut<<"Overload'+'-a=a+b:"<<a<<endl;a=a-b;cout<<"Overload'-'-a=a-b:"<<a<<endl;cout<<"重新用'<<','>>'輸入輸出:"<<endl;cout<<"輸入a的坐標(biāo):"cin>>a;cout<<"顯示<&l

17、t;a:"<<endl;cout<<a<<endl;cout<<"輸入b的坐標(biāo):"cin>>b;cout<<"顯示<<b:"<<endl;cout<<b<<endl;cout<<"*Overload '=' and '!='"<<endl;if(a=b)cout<<"a=b"<<endl;else if(a!

18、=b)cout<<"a!=b"<<endl;return 0;運(yùn)行結(jié)果:程序代碼:/*3、定義一個(gè)矩陣類(lèi)Matrix,均為M行N列,通過(guò)重載運(yùn)算符"+"、"-","<<",">>","+","-","=","!="來(lái)實(shí)現(xiàn)矩陣的相加、相減、輸出、輸入、自增、自減以及相等、不等的判斷。*/#include<iostream>using namespace std

19、;const int M=3;const int N=3;class Matrixprivate:int XMN;public:friend Matrix operator+(Matrix aN,Matrix bN);friend Matrix operator-(Matrix &a,Matrix &b);friend istream & operator>>(istream & input,Matrix &c);friend ostream & operator<<(istream & output,Matrix

20、 &c);friend Matrix operator+(Matrix &c);friend Matrix operator-(Matrix &c);friend int operator=(Matrix &a,Matrix &b);friend int operator!=(Matrix &a,Matrix &b);Matrix operator+(Matrix aN,Matrix bN) int i,j;Matrix cMN;for(i=0;i<M;i+)for(j=0;j<N;j+) cij=aij+bij;return

21、 cMN;Matrix operator-(Matrix *a,Matrix *b) int i,j;Matrix cMN;for(i=0;i<M;i+)for(j=0;j<N;j+) cij=aij-bij;return cMN;istream & operator>>(istream & input,Matrix *c)int i,j;for(i=0;i<M;i+)for(j=0;j<N;j+)input>>cij;ostream & operator<<(istream & output,Matr

22、ix *c)int i,j; for(i=0;i<M;i+)for(j=0;j<N;j+)output<<cij<<" "Matrix operator+(Matrix *c)int i,j;for(i=0;i<M;i+)for(j=0;j<N;j+)+cij;return cMN;Matrix operator-(Matrix *c)int i,j;for(i=0;i<M;i+)for(j=0;j<N;j+)-cij;return cMN;int operator=(Matrix *a,Matrix *b)int

23、 i,j;for(i=0;i<M;i+)for(j=0;j<N;j+)if(aij!=bij)return 0;return 1;int operator!=(Matrix *a,Matrix *b)int i,j;for(i=0;i<M;i+)for(j=0;j<N;j+)if(aij!=bij)return 1;return 0;int main()Matrix aMN,bMN,cMN;cout<<"<<,>>"<<endl;cin>>a;cin>>b;cout<<

24、;"Display(a):"<<a;cout<<"Display:(b)"<<b;cout<<"Overload + and -:"<<endl;cMN=aMN+MN;cout<<"Display(a):"<<c;cMN=aMN-MN;cout<<"Display:(b)"<<c;cout<<"Overload = and !=:"<<endl;

25、if(aMN=bMN)cout<<"aMN=bMN"<<endl;else cout<<"aMN!=bMN"<<endl;if(aMN!=bMN)cout<<"aMN!=bMN"<<endl;else cout<<"aMN=bMN"<<endl;return 0;4. 程序代碼:/*4、定義時(shí)間類(lèi)Time,時(shí)間的表示采用24小時(shí)制。重載運(yùn)算符"<<"和">>"

26、;實(shí)現(xiàn)時(shí)間的輸出和輸入;重載運(yùn)算符"+"和"-"實(shí)現(xiàn)時(shí)間推后和提前若干分鐘;重載運(yùn)算符"+"和"-"實(shí)現(xiàn)當(dāng)前時(shí)間推后和提前1小時(shí);重載">"、"<"、"="來(lái)判斷兩個(gè)時(shí)間之間大于、小于、等于以及不等于的關(guān)系。注意:輸入時(shí)需對(duì)數(shù)據(jù)合法性進(jìn)行判斷。*/#include<iostream.h>/using namespace std;class Time /Define class Timeprivate:int hour,minute,

27、second;public:Time operator-();Time operator+();Time operator+(int n);Time operator-(int n);friend istream & operator>>(istream & Input,Time & t);friend ostream & operator<<(ostream & Output,Time & t);friend int operator<(Time &a,Time &b);friend int ope

28、rator>(Time &a,Time &b);friend int operator=(Time &a,Time &b);istream & operator>>(istream & Input,Time & t) /Overload " >> " /Make sure hour,minute,second right.Input>>t.hour>>t.minute>>t.second;while(1) if(t.hour>24|t.hour&l

29、t;0|t.minute>60|t.minute<0|t.second>60|t.second<0)cout<<"Wrong! Please enter again!"<<endl; Input>>t.hour>>t.minute>>t.second;if(t.hour<24&&t.hour>0&&t.minute<60&&t.minute>0&&t.second<60|t.second>0)

30、break;return Input; ostream & operator<<(ostream & Output,Time & t) /Overload " << " Output<<t.hour<<":"<<t.minute<<":"<<t.second;return Output;Time Time:operator+(int n) /Overload " + " Time t;t.hour=hour;

31、 t.minute=minute+n;t.second=second;return t;Time Time:operator-(int n) /Overload " - "Time t; t.minute=minute-n;t.hour=hour;t.second=second;return t;Time Time:operator+() /Overload " + "Time t; t.minute=minute;t.second=second;+hour;t.hour=hour;return t;Time Time:operator-() /Over

32、load " - "Time t;t.minute=minute;t.second=second;-hour;t.hour=hour;return t;int operator<(Time &a,Time &b) /Overload " < "if(a.hour<b.hour)return 1;else if(a.hour=b.hour&&a.minute<b.minute)return 1;else if(a.hour=b.hour&&a.minute=b.minute&&

33、amp;a.second<b.second)return 1;else return 0;int operator>(Time &a,Time &b) /Overload " > "if(a.hour>b.hour)return 1;else if(a.hour=b.hour&&a.minute>b.minute)return 1;else if(a.hour=b.hour&&a.minute=b.minute&&a.second>b.second)return 1;else

34、 return 0;int operator=(Time &a,Time &b) /Overload " = "if(a.hour!=b.hour)return 0;else if(a.minute!=b.minute)return 0;elseif(a.second!=b.second)return 0;else return 1; int main()Time t,t0;cout<<"Please enter Time t(hour,minute,second):"cin>>t;cout<<&quo

35、t;*Display*"<<endl;cout<<"Time: "<<t<<endl;cout<<"*Operator*"<<endl;cout<<"Add 20 minutes: "t=t+20;cout<<t<<endl; cout<<"Minus 10 minutes: "t=t-10;cout<<t<<endl;cout<<"Add

36、1 hour: " +t;cout<<t<<endl;cout<<"Minus 1 hour: "-t;cout<<t<<endl;cout<<"Please enter Time t0(hour,minute,second):"cin>>t0;cout<<"*Display*"<<endl;cout<<"Time: "<<t0<<endl;cout<<

37、;"*Compare*"<<endl;if(t=t0)cout<<"t=t0"<<endl;elseif(t>t0)cout<<"t>t0"<<endl;elsecout<<"t<t0"<<endl;return 0;運(yùn)行結(jié)果:程序代碼:/*5、編寫(xiě)一個(gè)程序,處理一個(gè)復(fù)數(shù)與一個(gè)double型數(shù)相加的運(yùn)算,結(jié)果存放在一個(gè)double型的變量d1中,輸出d1的值,再以復(fù)數(shù)形式輸出此值。定義復(fù)數(shù)(Complex)類(lèi),在成

38、員函數(shù)中包含重載類(lèi)型轉(zhuǎn)換運(yùn)算符:Operator double() return real;*/#include<iostream.h>/using namespace std;class Complex /Define class Complexprivate:double real,imag;public:double Complex:operator+(double a);friend ostream & operator<<(ostream & output,Complex & a);friend istream & operat

39、or>>(istream & input,Complex & a);Complex() /構(gòu)造函數(shù)real=10;imag=10;Complex(double a) /定義轉(zhuǎn)換構(gòu)造函數(shù)real=a;imag=0;double Complex:operator+(double a) / 重載類(lèi)型轉(zhuǎn)換符“+”double b;b=a+real;return b;ostream & operator<<(ostream & output,Complex &a) /重載“ <<”output<<a.real<

40、<"+"<<a.imag<<"i"<<" "return output;istream & operator>>(istream & input,Complex &a) /重載“ >>”input>>a.real>>a.imag;return input;int main() / 主函數(shù)Complex s;double a,d1;cout<<"Complex: "<<s<&

41、lt;endl;cout<<"Please enter a double number(>>a):"cin>>a;d1=s+a;cout<<"d1= "<<d1<<endl;s=d1;cout<<"After s=d1 : "<<s<<endl;return 0;運(yùn)行結(jié)果:程序代碼:/*6、定義一個(gè)教師類(lèi)和一個(gè)學(xué)生類(lèi),二者有一部分?jǐn)?shù)據(jù)成員是相同的,例如num,name,sex。請(qǐng)編程,將一個(gè)學(xué)生類(lèi)對(duì)象轉(zhuǎn)換為教師類(lèi),只將以上3個(gè)相同的數(shù)據(jù)成員移植過(guò)去??梢栽O(shè)想為:一位學(xué)生大學(xué)畢業(yè)了,留校擔(dān)任教師,他原有的部分?jǐn)?shù)據(jù)對(duì)現(xiàn)在的教師身份來(lái)說(shuō)仍然是有用的,應(yīng)當(dāng)保留并成為其教師的數(shù)據(jù)的

溫馨提示

  • 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)論