C++程序設計實驗6參考模板_第1頁
C++程序設計實驗6參考模板_第2頁
C++程序設計實驗6參考模板_第3頁
C++程序設計實驗6參考模板_第4頁
C++程序設計實驗6參考模板_第5頁
已閱讀5頁,還剩3頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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

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

3、友元函數)。在main函數里定義復數對象,測試重載的這些運算符。#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對象輸入數據:" ;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)設計一個mystring類,包括數據成員char * pstr; 和 int length; 通過運算符重載實現字符串的輸入 >> 、輸出 << 、連接+=、賦值 = 、關系運算( = 、 != 、>、<)、下標等運算。#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 << "數組越界" << 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;實驗心得:本次實驗我們所學習的是函數的重載,函

溫馨提示

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

評論

0/150

提交評論