C++程序設(shè)計實驗6_第1頁
C++程序設(shè)計實驗6_第2頁
C++程序設(shè)計實驗6_第3頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、實驗6運算符重載實驗?zāi)康恼莆者\算符重載的規(guī)則;掌握運算符成員函數(shù)與運算符友元函數(shù)的實現(xiàn)及應(yīng)用;學(xué)會定義類中單目和雙目運算符的重載函數(shù);理解重載運算符的作用,學(xué)會對典型的運算符進行重載。實驗學(xué)時本次實驗需要2個學(xué)時。實驗要求實驗上機之前,根據(jù)實驗內(nèi)容要求,自行設(shè)計編寫程序,完成預(yù)習(xí)報告。 實驗上機時調(diào)試并修正程序。當(dāng)次上機結(jié)束前分析錯誤原因并給出實驗結(jié)論,提交實驗報告。實驗內(nèi)容1.基礎(chǔ)部分(1) 定義復(fù)數(shù)類complex,包括私有數(shù)據(jù)成員實部real和虛部images定義該 類的構(gòu)造,拷貝構(gòu)造,析構(gòu)函數(shù)。為該類重載運算符 +,-(友元函數(shù)),前置和 后置+,-(成員函數(shù)),插入符和提取符 , (

2、友元函數(shù))。在main函數(shù)里 定義復(fù)數(shù)對象,測試重載的這些運算符。2進階部分(2)設(shè)計一個 mystring類,包括數(shù)據(jù)成員 char * pstr;和int length;通過運算 符重載實現(xiàn)字符串的輸入 、輸出 、連接+=、賦值=、關(guān)系運算(=、!=、 )、下標(biāo)等運算。/* (1)定義復(fù)數(shù)類complex,包括私有數(shù)據(jù)成員實部 real和虛部image。定義該類的構(gòu)造, 拷貝構(gòu)造,析構(gòu)函數(shù)。為該類重載運算符 +,-(友元函數(shù)),前置和后置+,-(成員函數(shù)),插入符和提取符 , (友元函數(shù))。在main函數(shù)里定義復(fù)數(shù)對象,測試重載的這些運算符。#in clude<iostream>

3、;#in clude<stri ng>using n amespace std;class Complex public:Complex(i nt real1=O,i nt image1=0) :real(real1),image(image1)Complex() ;friend Complex operator+(c onst Complex &a1, const Complex &a2);friend Complex operator-(c onst Complex &a1, const Complex &a2);Complex operator

4、+();Complex operator+(i nt);Complex operator-。;Complex operator-(i nt);friend ostream& operator<<(ostream& os, const Complex&a3);friend istream& operator>>(istrea m& is.Complex&a3);private:int real;int image;Complex operator+(c onst Complex &a1, const Complex

5、&a2)retur n Complex(a1.real + a2.real, a1.image + a2.image);Complex operator-(c onst Complex &a1, const Complex &a2)return Complex(a1.real - a2.real, a1.image - a2.image);Complex Complex:operator+()+real;+image;return *this;Complex Complex:operator+(i nt)Complex a = *this;+(*this);return

6、 a;Complex Complex:operator-() -real;-image;return *this;Complex Complex:operator-(i nt)Complex a = *this;-(*this);return *this;ostream& operator<<(ostrea m& os, const Complex& a3)os<< a3.real << "+" << a3.image << "i"return os;istrea m&a

7、mp; operator»(istream& is, Complex&a3)is >> a3.real >> a3.image;return is;int mai n()Complex a4(4,5), a5(6,7),A,B;cout << "a4:" << a4 << en dl;cout << "a5:" << a5 << en dl;cout << "請重新為a4,a5對象輸入數(shù)據(jù):”;cin >&

8、gt; a4;cin >> a5;cout << "重新輸入后 a4: " << a4 << endl;cout << "重新輸入后 a5: " << a5 << endl;A = a4 + a5;cout << "重載修改后加法 A:"cout << A << en dl;A = a4 - a5;cout << "重載修改后減法 A:"cout << A <<

9、 en dl;cout <<"重載修改后 a4 前置 +:"<<+a4 << endl;cout <<"重載修改后 a5 后置 +:" <<a5+ << endl;cout << "重載修改后再次修改的a4前置-:"<< -a4 << endl;cout << "重載修改后再次修改的a5后置-:"<< a5- << endl;return 0;(2)設(shè)計一個 mystrin

10、g類,包括數(shù)據(jù)成員char * pstr;和int length;通過運算符重載實現(xiàn)字符串的輸入>>、輸出 <<、連接+=、賦值=、關(guān)系運算(=、!=、>、< )、下標(biāo)等運算。#i nclude <iostream>#in clude <stri ng>using n amespace std;class mystri ngpublic:mystri ng(char *p);mystri ng()free(pstr);mystri ng& operator=(mystri ng& s);void operator+=(

11、mystri ng & a)this->pstr = (char*)realloc(this->pstr, this->le ngth + a.len gth + 1); strcpy(this->pstr + (this->length), a.pstr);char& operator (i nt n);bool operator =(c onst mystri ng & s)if (strcmp(this->pstr, s.pstr) = 0)return 1;elsereturn 0;bool operator !=(c ons

12、t mystri ng & s)if (strcmp(this->pstr, s.pstr) != 0)return 1;elsereturn 0;bool operator <(c onst mystri ng & s)if (strcmp(this->pstr, s.pstr)<0)return 1;elsereturn 0;bool operator >(c onst mystri ng & s)if (strcmp(this->pstr, s.pstr)>0) return 1;elsereturn 0;friend o

13、stream & operator <<(ostream & os, const mystring & s) return os << s.pstr << strle n(s.pstr); friend istream & operator >>(istream & is, mystri ng & s)delete s.pstr;is >>s .len gth ;s.pstr = new chars .len gth + 1;is >> s.pstr;*(s.pstr + s

14、.le ngth) = '0:return is;private:char *pstr;int len gth;mystri ng:mystri ng(char *p)this->pstr = (char*)malloc(strle n(p) + 1); strcpy(this->pstr, p);this->le ngth = strle n(p);mystri ng& mystri ng: operator =(mystri ng & s)deletepstr; this->pstr = new charstrle n( s.pstr) +

15、1; if (pstr)strcpy(this->pstr, s.pstr); return *this;char& mystri ng:operator(i nt n)static char ch = 0;if (n > length | n < 0)cout << "數(shù)組越界"<< endl; return ch;elsereturn *(pstr + n);int mai n()mystring a("abde"), b("defe");/mystri ng c(a);/cout

16、 << c;cout << a << "" << b << en dl;a = b;cout << a << en dl;a += b;cout << a << en dl;cin >> a;cout << a << en dl;if (a > b)cout << "a 字符串更大:"<< a << endl;if (a < b)cout << "b 字符串更大:"<< b << endl;if (a = b)cout << "a,b 相等"<< a << endl;if (a != b)cout << "a,b 不相等"<< endl;a1 = ' q'cout << a << en dl;實驗心得:本次實驗我們所學(xué)習(xí)的是函數(shù)的重載,函數(shù) 的重載是為了滿足不同環(huán)境下調(diào)用函數(shù)不同的問題。比如求 最大值,我們?nèi)齻€值求最大值,兩個值求更大值。在這里運 用

溫馨提示

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

最新文檔

評論

0/150

提交評論