實驗五-運算符重載--完成_第1頁
實驗五-運算符重載--完成_第2頁
實驗五-運算符重載--完成_第3頁
實驗五-運算符重載--完成_第4頁
實驗五-運算符重載--完成_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

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

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

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

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

5、 std;class Complex /定義復數(shù)類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<<"請輸入復數(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<<"請輸入需要與復數(shù)相減的實數(shù):"cin>>m; c=c-m;c.Display();return 0;運行結(jié)果:2.程序代碼:/*2、定義點類Point,重載運算符""、""、""、"!="、&

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

9、mespace std;class Point /定義point類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; /注意賦值時沒有static," int static Point:count=0; " wrong!void Point:Input()cout<<"*輸入*"<<endl;cout<<"請輸入點坐標(x,y)"cin>>x>>y;count+;void Point:Display()cout<<"*顯示*"<<endl;cout<<"第"<<count<<

12、"個坐標為:"<<"("<<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) /重載流插入運算

15、符input>>a.x>>a.y;return input;ostream & operator<<(ostream & output,Point & a) /重載流插入運算符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的坐標:"cin>>a;cout<<"顯示<&l

17、t;a:"<<endl;cout<<a<<endl;cout<<"輸入b的坐標:"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;運行結(jié)果:程序代碼:/*3、定義一個矩陣類Matrix,均為M行N列,通過重載運算符"+"、"-","<<",">>","+","-","=","!="來實現(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、定義時間類Time,時間的表示采用24小時制。重載運算符"<<"和">>"

26、;實現(xiàn)時間的輸出和輸入;重載運算符"+"和"-"實現(xiàn)時間推后和提前若干分鐘;重載運算符"+"和"-"實現(xiàn)當前時間推后和提前1小時;重載">"、"<"、"="來判斷兩個時間之間大于、小于、等于以及不等于的關(guān)系。注意:輸入時需對數(shù)據(jù)合法性進行判斷。*/#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;運行結(jié)果:程序代碼:/*5、編寫一個程序,處理一個復數(shù)與一個double型數(shù)相加的運算,結(jié)果存放在一個double型的變量d1中,輸出d1的值,再以復數(shù)形式輸出此值。定義復數(shù)(Complex)類,在成

38、員函數(shù)中包含重載類型轉(zhuǎ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) / 重載類型轉(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;運行結(jié)果:程序代碼:/*6、定義一個教師類和一個學生類,二者有一部分數(shù)據(jù)成員是相同的,例如num,name,sex。請編程,將一個學生類對象轉(zhuǎn)換為教師類,只將以上3個相同的數(shù)據(jù)成員移植過去。可以設(shè)想為:一位學生大學畢業(yè)了,留校擔任教師,他原有的部分數(shù)據(jù)對現(xiàn)在的教師身份來說仍然是有用的,應(yīng)當保留并成為其教師的數(shù)據(jù)的

溫馨提示

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

評論

0/150

提交評論