




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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>#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 Complex
4、&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;Complex o
5、perator+(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:oper
6、ator+(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.image &l
7、t;< "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 << "重载
9、修改后减法A:"cout << A << endl;cout <<"重载修改后a4前置+:"<<+a4 << endl;cout <<"重载修改后a5后置+:" <<a5+ << endl;cout << "重载修改后再次修改的a4前置-:" << -a4 << endl;cout << "重载修改后再次修改的a5后置-:" << a5- <&l
10、t; 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& operator=(
11、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;elseretur
12、n 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 1;el
13、sereturn 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.pstr
14、;*(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.pstr
15、) + 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);/cout
16、<< 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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 移动设备租赁市场用户行为研究考核试卷
- 烟草生产线自动化控制技术考核试卷
- 管道工程行业政策导向与发展趋势考核试卷
- 船舶货运与物流供应链整合考核试卷
- 球类产品智能制造与工业考核试卷
- 航空公司航班运行数据分析考核试卷
- 清扫工具销售与渠道拓展策略考核试卷
- 腈纶纤维制造考核试卷
- 机器人服务行业智能语音交互技术考核试卷
- 版权评估运营补充协议
- 华大新高考联盟2025届高三4月教学质量测评化学+答案
- 2025年中国防晒护理洗发露市场调查研究报告
- 建筑材料租赁标准合同范本7篇
- 2025-2030中国太阳能照明系统行业市场发展趋势与前景展望战略研究报告
- 国家电网招聘考试(金融类)专业考试历年真题及答案
- 2025年湖北省汉江国有资本投资集团有限公司招聘笔试参考题库含答案解析
- 2025年高考政治三轮冲刺复习:统编版选择性必修3《逻辑与思维》开放类主观题 提分刷题练习题(含答案)
- 铁路雨季三防培训课件
- 大学英语四级考试2024年12月真题(第一套)Part I Writing
- (部编版)语文四年级上册课外阅读“天天练”100篇,附参考答案
- 静疗护理典型案例
评论
0/150
提交评论