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

下載本文檔

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

文檔簡介

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

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

3、in clude<stri ng>using n ames pace std;class Comp lexp ublic:Comp lex(i nt real1=0,i nt image1=0) :real(real1),image(image1)Com pl ex() ;friend Complex op erator+(c onst Complex &a1, const Comp lex &a2);friend Complex op erator-(c onst Complex &a1, const Complex &a2);Comp lex o

4、p erator+();Comp lex op erator+(i nt);Comp lex op erator-();Comp lex op erator-(i nt);friend ostream& op erator<<(ostream& os, const Complex&a3);friend istream& op erator>>(istrea m& is,Complex&a3);p rivate:int real;int image;Comp lex op erator+(c onst Complex &am

5、p;a1, const Complex &a2)retur n CompI ex(a1.real + a2.real, a1.image + a2.image);Comp lex op erator-(c onst Comp lex &a1, const Complex &a2)retur n CompI ex(a1.real - a2.real, a1.image - a2.image);Comp lex Comp lex:o perator+()+real;+image;return *this;Comp lex Comp lex:o perator+(i nt)C

6、omp lex a = *this;+(*this);return a;Comp lex Comp lex:o perator-()-real;-image;return *this;Comp lex Comp lex:o perator-(i nt)Comp lex a = *this;-(*this);return *this;ostream& op erator<<(ostrea m& os, const Complex& a3)os<< a3.real << "+" << a3.image &l

7、t;< "i"return os;istrea m& op erator>>(istream& is, Complex&a3)is >> a3.real >> a3.image;return is;int mai n()Comp lex a4(4,5), a5(6,7),A,B;cout << "a4:" << a4 << en dl;cout << "a5:" << a5 << en dl;cou

8、t << "請重新為a4,a5對象輸入數(shù)據(jù):cin >> a4;cin >> a5;cout << "重新輸入后a4:"<< a4 << en dl;cout << "重新輸入后a5:"<< a5 << en dl;A = a4 + a5;cout << "重載修改后加法A:"cout << A << en dl;A = a4 - a5;cout << "重載修

9、改后減法A:"cout << A << en dl;cout <<"重載修改后 a4 前置 +:"<<+a4 << endl;cout <<"重載修改后 a5 后置 +:" <<a5+ << endl;cout << "重載修改后再次修改的a4前置-:"<< -a4 << endl;cout << "重載修改后再次修改的a5后置-:"<< a5- &l

10、t;< endl;return 0;(2)設(shè)計一個 mystring類,包括數(shù)據(jù)成員 char * pstr;和!=、int length;通過運算符重載實現(xiàn)字符串的輸入>>、輸出 <<、連接+=、賦值關(guān)系運算(=、!=、>、<)、下標等運算。#in clude <iostream> #in clude <stri ng> using n ames pace std;class mystri ng public: mystri ng(char *p);mystri ng() free( pstr);mystri ng&

11、op erator=(mystri ng& s);void op erator+=(mystn ng & a) this->p str = (char*)realloc(this->p str, this->le ngth + a.le ngth + 1);strc py(this->p str + (this->le ngth), a.p str);char& op erator (i nt n);bool op erator =(c onst mystri ng & s) if (strcm p(this->p str,

12、s.p str) = 0) return 1;else return 0;bool op erator !=(c onst mystri ng & s) if (strcm p(this->p str, s.pstr) != 0) return 1;else return 0;bool op erator <(c onst mystri ng & s) if (strcm p(this->p str, s.p str)<0) return 1;else return 0;bool op erator >(c onst mystri ng &

13、 s) if (strcm p(this->p str, s.p str)>0) return 1;else return 0;friend ostream & operator <<(ostream & os, const mystring & s) return os << s.pstr <<strle n(s. pstr); friend istream & op erator >>(istream & is, mystri ng & s) delete s .p str;is &

14、gt;>s.le ngth ;s.p str = new chars .len gth + 1;is >> s.p str;*(s .p str + s.le ngth) = '0'return is;private: char *p str;int len gth;mystri ng:mystri ng(char *p) this->p str = (char*)malloc(strle n(p) + 1);strc py (this->p str, p);this->le ngth = strle n(p);mystri ng&

15、mystri ng: op erator =(mystri ng & s) delete pstr; this->p str = new charstrle n(s.pstr) + 1;if (p str)strc py(this-> pstr, s.p str); return *this;char& mystri ng:o perator(i nt n) static char ch = 0;if (n > len gth | n < 0) cout << "數(shù)組越界"<< endl;return ch;e

16、lse return *( pstr + n);int mai n() mystri ng a("abde"), b("defe");/mystri ng c(a);/cout << c;cout <<<<""<< b << endl;a = b;cout <<<<endl;a += b;cout <<<<endl;cin >> a;cout << a<<endl;if (a > b)c

17、out << "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;實驗心得:本次實驗我們所學習的是函數(shù)的重載,函數(shù) 的重

溫馨提示

  • 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

提交評論