




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí)驗(yàn)6 運(yùn)算符重載實(shí)驗(yàn)?zāi)康膌 掌握運(yùn)算符重載的規(guī)則;l 掌握運(yùn)算符成員函數(shù)與運(yùn)算符友元函數(shù)的實(shí)現(xiàn)及應(yīng)用;l 學(xué)會(huì)定義類中單目和雙目運(yùn)算符的重載函數(shù);l 理解重載運(yùn)算符的作用,學(xué)會(huì)對(duì)典型的運(yùn)算符進(jìn)行重載。實(shí)驗(yàn)學(xué)時(shí)本次實(shí)驗(yàn)需要2個(gè)學(xué)時(shí)。實(shí)驗(yàn)要求l 實(shí)驗(yàn)上機(jī)之前,根據(jù)實(shí)驗(yàn)內(nèi)容要求,自行設(shè)計(jì)編寫(xiě)程序,完成預(yù)習(xí)報(bào)告。l 實(shí)驗(yàn)上機(jī)時(shí)調(diào)試并修正程序。l 當(dāng)次上機(jī)結(jié)束前分析錯(cuò)誤原因并給出實(shí)驗(yàn)結(jié)論,提交實(shí)驗(yàn)報(bào)告。實(shí)驗(yàn)內(nèi)容1.基礎(chǔ)部分(1)定義復(fù)數(shù)類complex,包括私有數(shù)據(jù)成員實(shí)部real和虛部image。定義該類的構(gòu)造,拷貝構(gòu)造,析構(gòu)函數(shù)。為該類重載運(yùn)算符+,-(友元函數(shù)),前置和后置+,-(成員函數(shù)),插
2、入符和提取符<<,>>(友元函數(shù))。在main函數(shù)里定義復(fù)數(shù)對(duì)象,測(cè)試重載的這些運(yùn)算符。2.進(jìn)階部分(2)設(shè)計(jì)一個(gè)mystring類,包括數(shù)據(jù)成員char * pstr; 和 int length; 通過(guò)運(yùn)算符重載實(shí)現(xiàn)字符串的輸入>>、輸出<<、連接+=、賦值=、關(guān)系運(yùn)算(=、!=、>、<)、下標(biāo)等運(yùn)算。/*(1)定義復(fù)數(shù)類complex,包括私有數(shù)據(jù)成員實(shí)部real和虛部image。定義該類的構(gòu)造,拷貝構(gòu)造,析構(gòu)函數(shù)。為該類重載運(yùn)算符+,-(友元函數(shù)),前置和后置+,-(成員函數(shù)),插入符和提取符<<,>>(
3、友元函數(shù))。在main函數(shù)里定義復(fù)數(shù)對(duì)象,測(cè)試重載的這些運(yùn)算符。#include<iostream>1 / 8#include<string>using namespace std;class Complex 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 Com
4、plex &a1, const Complex &a2);Complex operator+();Complex operator+(int);Complex operator-();Complex operator-(int);friend ostream& operator<<(ostream& os, const Complex&a3);friend istream& operator>>(istream& is, Complex&a3);private:int real;int image;Comp
5、lex 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 Complex:operator+()+real;+image;return *this;Complex Complex
6、: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.ima
7、ge << "i"return os;istream& operator>>(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
8、 << "請(qǐng)重新為a4,a5對(duì)象輸入數(shù)據(jù):" ;cin >> a4;cin >> a5;cout << "重新輸入后a4:" << a4 << endl;cout << "重新輸入后a5:" << a5 << endl;A = a4 + a5;cout << "重載修改后加法A:"cout << A << endl;A = a4 - a5;cout << &qu
9、ot;重載修改后減法A:"cout << A << endl;cout <<"重載修改后a4前置+:"<<+a4 << endl;cout <<"重載修改后a5后置+:" <<a5+ << endl;cout << "重載修改后再次修改的a4前置-:" << -a4 << endl;cout << "重載修改后再次修改的a5后置-:" << a5- &
10、lt;< endl;return 0;(2)設(shè)計(jì)一個(gè)mystring類,包括數(shù)據(jù)成員char * pstr; 和 int length; 通過(guò)運(yùn)算符重載實(shí)現(xiàn)字符串的輸入 >> 、輸出 << 、連接+=、賦值 = 、關(guān)系運(yùn)算( = 、 != 、>、<)、下標(biāo)等運(yùn)算。#include <iostream>#include <string>using namespace std;class mystringpublic:mystring(char *p);mystring()free(pstr);mystring& opera
11、tor=(mystring& s);void operator+=(mystring & a)this->pstr = (char*)realloc(this->pstr, this->length + 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;else
12、return 0;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 >(const mystring & s)if (strcmp(this->pstr, s.pstr)>0)return
13、 1;elsereturn 0;friend ostream & operator <<(ostream & os, const mystring & s) return os << s.pstr << strlen(s.pstr); friend istream & operator >>(istream & is, mystring & s)delete s.pstr;is >>s.length ;s.pstr = new chars.length + 1;is >> s
14、.pstr;*(s.pstr + s.length) = '0'return is;private:char *pstr;int length;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
15、.pstr) + 1;if (pstr)strcpy(this->pstr, s.pstr); return *this;char& mystring:operator(int n)static char 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);/
16、cout << c;cout << a << " " << b << endl;a = b;cout << a << endl;a += b;cout << a << endl;cin >> a;cout << a << endl;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 << endl;實(shí)驗(yàn)心得:本次實(shí)驗(yàn)我們所學(xué)習(xí)的是函數(shù)的重載,函
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030全球及中國(guó)廂式貨車(chē)改裝行業(yè)產(chǎn)業(yè)運(yùn)行態(tài)勢(shì)及投資規(guī)劃深度研究報(bào)告
- 寵物公益活動(dòng)活動(dòng)方案
- 西安電子科技大學(xué)《基礎(chǔ)西班牙語(yǔ)(II)》2023-2024學(xué)年第一學(xué)期期末試卷
- 2025至2030中國(guó)鴕鳥(niǎo)養(yǎng)殖行業(yè)發(fā)展趨勢(shì)分析與未來(lái)投資戰(zhàn)略咨詢研究報(bào)告
- 2025至2030中國(guó)面粉行業(yè)供需形勢(shì)與發(fā)展前景展望研究報(bào)告
- 常德科技職業(yè)技術(shù)學(xué)院《生物醫(yī)藥專業(yè)英語(yǔ)》2023-2024學(xué)年第一學(xué)期期末試卷
- 廣東潮州衛(wèi)生健康職業(yè)學(xué)院《社會(huì)化閱讀》2023-2024學(xué)年第一學(xué)期期末試卷
- 榆林職業(yè)技術(shù)學(xué)院《心理統(tǒng)計(jì)學(xué)(2)》2023-2024學(xué)年第一學(xué)期期末試卷
- 溫州職業(yè)技術(shù)學(xué)院《醫(yī)學(xué)物理學(xué)》2023-2024學(xué)年第一學(xué)期期末試卷
- 09月嫂考試題庫(kù)及答案
- 建筑工程資料填寫(xiě)范例與指南
- 公益性公墓建設(shè)實(shí)施方案(3篇)
- 2023年貴州貴州賴茅酒業(yè)有限公司招聘考試真題
- 操作系統(tǒng)-001-國(guó)開(kāi)機(jī)考復(fù)習(xí)資料
- 合并財(cái)務(wù)報(bào)表格式(2019版)
- 心臟射頻消融術(shù)
- 《商務(wù)郵件禮儀》課件
- 《配電自動(dòng)化系統(tǒng)》課件
- 創(chuàng)業(yè)基礎(chǔ)理論與實(shí)務(wù)(寧波財(cái)經(jīng)學(xué)院)知到智慧樹(shù)章節(jié)答案
- 《某飛機(jī)場(chǎng)物業(yè)管理服務(wù)方案》
- 《讓子彈飛》電影賞析
評(píng)論
0/150
提交評(píng)論