C++程序設(shè)計實(shí)驗(yàn)6參考模板_第1頁
C++程序設(shè)計實(shí)驗(yàn)6參考模板_第2頁
C++程序設(shè)計實(shí)驗(yàn)6參考模板_第3頁
C++程序設(shè)計實(shí)驗(yàn)6參考模板_第4頁
C++程序設(shè)計實(shí)驗(yàn)6參考模板_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

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é)會定義類中單目和雙目運(yùn)算符的重載函數(shù);l 理解重載運(yùn)算符的作用,學(xué)會對典型的運(yùn)算符進(jìn)行重載。實(shí)驗(yàn)學(xué)時本次實(shí)驗(yàn)需要2個學(xué)時。實(shí)驗(yàn)要求l 實(shí)驗(yàn)上機(jī)之前,根據(jù)實(shí)驗(yàn)內(nèi)容要求,自行設(shè)計編寫程序,完成預(yù)習(xí)報告。l 實(shí)驗(yàn)上機(jī)時調(diào)試并修正程序。l 當(dāng)次上機(jī)結(jié)束前分析錯誤原因并給出實(shí)驗(yàn)結(jié)論,提交實(shí)驗(yàn)報告。實(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ù)對象,測試重載的這些運(yùn)算符。2.進(jìn)階部分(2)設(shè)計一個mystring類,包括數(shù)據(jù)成員char * pstr; 和 int length; 通過運(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ù)對象,測試重載的這些運(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、 << "請重新為a4,a5對象輸入數(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è)計一個mystring類,包括數(shù)據(jù)成員char * pstr; 和 int length; 通過運(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. 本站所有資源如無特殊說明,都需要本地電腦安裝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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論