![C++程序设计实验6_第1页](http://file2.renrendoc.com/fileroot_temp3/2021-11/2/56d2739e-8f1d-4b27-8d22-c0c28d4acb31/56d2739e-8f1d-4b27-8d22-c0c28d4acb311.gif)
![C++程序设计实验6_第2页](http://file2.renrendoc.com/fileroot_temp3/2021-11/2/56d2739e-8f1d-4b27-8d22-c0c28d4acb31/56d2739e-8f1d-4b27-8d22-c0c28d4acb312.gif)
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 实验 6 运算符重载 实验目的 掌握运算符重载的规则; 掌握运算符成员函数与运算符友元函数的实现及应用 ; 学会定义类中单目与双目运算符的重载函数; 理解重载运算符的作用,学会对典型的运算符进行重载。 实验学时 本次实验需要 2 个学时。 实验要求 实验上机之前,根据实验内容要求,自行设计编写程序,完成预习报告。 实验上机时调试并修正程序。 当次上机结束前分析错误原因并给出实验结论,提交实验报告。 实验内容 1、 基础部分 (1) 定义复数类 complex,包括私有数据成员实部 real 与虚部 image。定义该类的 构造,拷贝构造祈构函数。为该类重载运算符+,-(友元函数),前置与后置
2、+,-(成员 函数),插入符与提取符, (友元函数)。在 main 函数里定义复数对象,测试重载 的这些运算符。 2、 进阶部分 (2) 设计一个 mystri ng 类,包括数据成员 char * pstr;与 in tie ngth;通过运算符重 载实现字符串的输入 、输出 、连接+=、赋值=、关系运算(=、!=、)、 下标等运算。 /*(1)定义复数类 complex,包括私有数据成员实部 real 与虚部 image。定义该类的构造,拷贝 构造,析构函数。 为该类重载运算符 +,-(友元函数),前置与后置+,-(成员函数),插入符与提取符,(友元 函数)。 在 main函数里定义复数对
3、象,测试重载的这些运算符。 #in clude #in clude using n amespace std; class Complex public: Complex(i nt real1=O,i nt image1=0) :real(real1),image(image1) Complex() ; friend Complex operator+(c onst Complex &a1, const Complex &a2); friend Complex operator-(c onst Complex &a1, const Complex &a2); C
4、omplex operator+(); Complex operator+(i nt); Complex operator-。; Complex operator-(i nt); friend ostream& operator(ostream& os, const Complex&a3); friend istream& operator(istrea m& is. Complex&a3); private: int real; int image; ; Complex operator+(c onst Complex &a1, con
5、st Complex &a2) return Complex(a1、real + a2、real, a1、image + a2、image); Complex operator-(c onst Complex &a1, const Complex &a2) return Complex(a1、real - a2、 real, a1、 image - a2、 image); Complex Complex:operator+() +real; +image; return *this; Complex Complex:operator+(i nt) Complex a =
6、 *this; +(*this); return a; Complex Complex:operator-() -real; -image; return *this; Complex Complex:operator-(i nt) Complex a = *this; -(*this); return *this; ostream& operator(ostrea m& os, const Complex& a3) os a3、real + a3、image a3、real a3、image; return is; int mai n() Complex a4(4,5
7、), a5(6,7),A,B; cout a4: a4 en dl; cout a5: a5 en dl; cout a4; cin a5; cout 重新输入后 a4: a4 endl; cout 重新输入后 a5: a5 endl; A = a4 + a5; cout 重载修改后加法 A:; cout A en dl; A = a4 - a5; cout 重载修改后减法 A:; cout A en dl; cout 重载修改后 a4 前置 +:+a4 endl; cout 重载修改后 a5 后置 +: a5+ endl; cout 重载修改后再次修改的 a4 前置-: -a4 endl;
8、cout 重载修改后再次修改的 a5 后置-: a5- 、输出 、)、下标等运算。 #i nclude #in clude using n amespace std; class mystri ng public: mystri ng(char *p); mystri ng() free(pstr); mystri ng& operator=(mystri ng& s); void operator+=(mystri ng & a) this-pstr = (char*)realloc(this-pstr, this-length + a 、 length + 1);
9、strcpy(this-pstr + (this-length), a 、 pstr); char& operator (i nt n); bool operator =(c onst mystri ng & s) if (strcmp(this-pstr, s 、 pstr) = 0) return 1; else return 0; bool operator !=(c onst mystri ng & s) if (strcmp(this-pstr, s 、 pstr) != 0) return 1; else return 0; bool operator ps
10、tr, s 、 pstr)(c onst mystri ng & s) if (strcmp(this-pstr, s 、 pstr)0) return 1; else return 0; friend ostream & operator (ostream & os, const mystring & s) return os s、pstr (istream & is, mystri ng & s) delete s、pstr; is s、length ; s、pstr = new chars、length + 1; is s、pstr; *(
11、s、pstr + s、length) = 0: return is; private: char *pstr; int len gth; ; mystri ng:mystri ng(char *p) this-pstr = (char*)malloc(strle n(p) + 1); strcpy(this-pstr, p); this-le ngth = strle n(p); mystri ng& mystri ng: operator =(mystri ng & s) deletepstr; this-pstr = new charstrlen(s、pstr) + 1;
12、if (pstr)strcpy(this-pstr, s、pstr); return *this; char& mystri ng:operator(i nt n) static char ch = 0; if (n length | n 0) cout 数组越界 endl; return ch; else return *(pstr + n); int mai n() mystring a(abde), b(defe); /mystri ng c(a); /cout c; cout a b en dl; a = b; cout a en dl; 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 en dl; 实验心得:本次实验我们所学习的就是函数的重载 ,函数 的重载就是为了满足不同环境下调用函数不同的问题。比如 求最大值,我们三个值求最大值,两个值求更大值。在这里运 用函数的重载我只需要定义一个函数为 max,采用不
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 配音演员聘用合同范本
- 探索在线技能培训的新模式
- 指点迷津筑梦未来主题班会
- 技术进口合同范本
- 2025君华御御庭消防安装工程合同
- 框架合同模板框架协议范本简单
- 2025年人教A版八年级生物上册月考试卷含答案
- 成本控制与财务分析
- 2025年浙科版选修五历史下册阶段测试试卷
- 开发创业精神的技能训练
- 六年级上册数学书苏教版答案
- 2023年全国中小学思政课教师网络培训研修总结心得体会
- CDE网站申请人之窗栏目介绍及用户操作手册
- 车班班长工作总结5篇
- 行业会计比较(第三版)PPT完整全套教学课件
- 值机业务与行李运输实务(第3版)高职PPT完整全套教学课件
- 高考英语语法填空专项训练(含解析)
- 42式太极剑剑谱及动作说明(吴阿敏)
- 部编版语文小学五年级下册第一单元集体备课(教材解读)
- 仁爱英语九年级下册单词表(中英文)
- 危险化学品企业安全生产标准化课件
评论
0/150
提交评论