




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、實驗6 運(yùn)算符重載實驗?zāi)康膌 掌握運(yùn)算符重載的規(guī)則;l 掌握運(yùn)算符成員函數(shù)與運(yùn)算符友元函數(shù)的實現(xiàn)及應(yīng)用;l 學(xué)會定義類中單目和雙目運(yùn)算符的重載函數(shù);l 理解重載運(yùn)算符的作用,學(xué)會對典型的運(yùn)算符進(jìn)行重載。實驗學(xué)時本次實驗需要2個學(xué)時。實驗要求l 實驗上機(jī)之前,根據(jù)實驗內(nèi)容要求,自行設(shè)計編寫程序,完成預(yù)習(xí)報告。l 實驗上機(jī)時調(diào)試并修正程序。l 當(dāng)次上機(jī)結(jié)束前分析錯誤原因并給出實驗結(jié)論,提交實驗報告。實驗內(nèi)容1.基礎(chǔ)部分(1)定義復(fù)數(shù)類complex,包括私有數(shù)據(jù)成員實部real和虛部image。定義該類的構(gòu)造,拷貝構(gòu)造,析構(gòu)函數(shù)。為該類重載運(yùn)算符+,-(友元函數(shù)),前置和后置+,-(成員函數(shù)),插
2、入符和提取符(友元函數(shù))。在main函數(shù)里定義復(fù)數(shù)對象,測試重載的這些運(yùn)算符。2.進(jìn)階部分(2)設(shè)計一個mystring類,包括數(shù)據(jù)成員char * pstr; 和 int length; 通過運(yùn)算符重載實現(xiàn)字符串的輸入、輸出、)、下標(biāo)等運(yùn)算。/*(1)定義復(fù)數(shù)類complex,包括私有數(shù)據(jù)成員實部real和虛部image。定義該類的構(gòu)造,拷貝構(gòu)造,析構(gòu)函數(shù)。為該類重載運(yùn)算符+,-(友元函數(shù)),前置和后置+,-(成員函數(shù)),插入符和提取符(友元函數(shù))。在main函數(shù)里定義復(fù)數(shù)對象,測試重載的這些運(yùn)算符。#include#includeusing namespace std;class Comp
3、lex public:Complex(int real1=0,int image1=0) :real(real1),image(image1)Complex() ;friend Complex operator+(const Complex &a1, const Complex &a2);friend Complex operator-(const Complex &a1, const Complex &a2);Complex operator+();Complex operator+(int);Complex operator-();Complex operator-(int);friend
4、 ostream& operator(istream& is, Complex&a3);private:int real;int image;Complex operator+(const Complex &a1, const Complex &a2)return Complex(a1.real + a2.real, a1.image + a2.image);Complex operator-(const Complex &a1, const Complex &a2)return Complex(a1.real - a2.real, a1.image - a2.image);Complex C
5、omplex:operator+()+real;+image;return *this;Complex Complex:operator+(int)Complex a = *this;+(*this);return a;Complex Complex:operator-()-real;-image;return *this;Complex Complex:operator-(int)Complex a = *this;-(*this);return *this;ostream& operator(ostream& os, const Complex& a3)os a3.real + a3.im
6、age (istream& is, Complex&a3)is a3.real a3.image;return is;int main()Complex a4(4,5), a5(6,7),A,B;cout a4: a4 endl;cout a5: a5 endl;cout a4;cin a5;cout 重新輸入后a4: a4 endl;cout 重新輸入后a5: a5 endl;A = a4 + a5;cout 重載修改后加法A:;cout A endl;A = a4 - a5;cout 重載修改后減法A:;cout A endl;cout 重載修改后a4前置+:+a4 endl;cout 重
7、載修改后a5后置+: a5+ endl;cout 重載修改后再次修改的a4前置-: -a4 endl;cout 重載修改后再次修改的a5后置-: a5- 、輸出 、)、下標(biāo)等運(yùn)算。#include #include using namespace std;class mystringpublic:mystring(char *p);mystring()free(pstr);mystring& operator=(mystring& s);void operator+=(mystring & a)this-pstr = (char*)realloc(this-pstr, this-length
8、+ a.length + 1);strcpy(this-pstr + (this-length), a.pstr);char& operator (int n);bool operator =(const mystring & s)if (strcmp(this-pstr, s.pstr) = 0)return 1;elsereturn 0;bool operator !=(const mystring & s)if (strcmp(this-pstr, s.pstr) != 0)return 1;elsereturn 0;bool operator pstr, s.pstr)(const m
9、ystring & s)if (strcmp(this-pstr, s.pstr)0)return 1;elsereturn 0;friend ostream & operator (ostream & os, const mystring & s) return os s.pstr (istream & is, mystring & s)delete s.pstr;is s.length ;s.pstr = new chars.length + 1;is s.pstr;*(s.pstr + s.length) = 0;return is;private:char *pstr;int leng
10、th;mystring:mystring(char *p)this-pstr = (char*)malloc(strlen(p) + 1);strcpy(this-pstr, p);this-length = strlen(p);mystring& mystring: operator =(mystring & s)deletepstr; this-pstr = new charstrlen(s.pstr) + 1;if (pstr)strcpy(this-pstr, s.pstr); return *this;char& mystring:operator(int n)static char
11、 ch = 0;if (n length | n 0)cout 數(shù)組越界 endl;return ch;elsereturn *(pstr + n);int main()mystring a(abde), b(defe);/mystring c(a);/cout c;cout a b endl;a = b;cout a endl;a += b;cout a a;cout 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 endl;實驗心得:本次實驗我們所學(xué)習(xí)的是函數(shù)的重載,函數(shù)的重載是為了滿足不同環(huán)境下調(diào)用函數(shù)不同的問題。比如求最大值,我們?nèi)齻€值求最大值,兩個值求更大值。在這里運(yùn)用函數(shù)的重載我只需要定義一個函數(shù)為max,采用不同個數(shù)的形參就可
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 清淤項目可行性分析報告
- 2023-2029年中國威士忌酒行業(yè)市場發(fā)展現(xiàn)狀及投資策略咨詢報告
- 刻字筆行業(yè)深度研究分析報告(2024-2030版)
- 中國紅外線夜視攝錄機(jī)行業(yè)市場發(fā)展監(jiān)測及投資策略研究報告
- 中國玩具市場競爭格局及投資戰(zhàn)略規(guī)劃報告
- 中國法式門冰箱行業(yè)市場運(yùn)營現(xiàn)狀及投資方向研究報告
- 品字尾輸出公插項目投資可行性研究分析報告(2024-2030版)
- 瓷磚檢測培訓(xùn)課件
- 中國毛絨收納桶行業(yè)市場發(fā)展前景及發(fā)展趨勢與投資戰(zhàn)略研究報告(2024-2030)
- 網(wǎng)絡(luò)運(yùn)營培訓(xùn)課件
- 火災(zāi)防治、隱患辨識與應(yīng)急避險
- 2025年河北省滄州市初中學(xué)業(yè)水平摸底考試地理試卷(含答案)
- 危重患者血糖管理指南
- 交通管理與控制知到智慧樹章節(jié)測試課后答案2024年秋同濟(jì)大學(xué)
- 磷酸鐵鋰正極材料及鋰離子電池電池項目可行性研究報告
- 川劇變臉模板
- DB11-T 382-2017 建設(shè)工程監(jiān)理規(guī)程
- 小紅書認(rèn)證商標(biāo)授權(quán)書
- 課程設(shè)計數(shù)學(xué)建模案例
- 2025年池州市投資控股集團(tuán)招聘筆試參考題庫含答案解析
- 國家標(biāo)準(zhǔn)化代謝性疾病管理中心(MMC)及管理指南介紹(完整版)
評論
0/150
提交評論