

下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 城镇污水管网建设项目建设管理方案(参考)
- xx河流排水防涝设施建设项目质量管理方案(参考范文)
- 2025年非离子型纤维素醚项目合作计划书
- 宪法知识学习题库
- 2025年天猫养车项目发展计划
- 下关穴治疗疼痛的现代技术融合
- 无人驾驶电动拖拉机平台的设计及试验
- 现代泌尿肿瘤学阅读笔记
- 2025年GPS高空探测系统项目发展计划
- 文化旅游的发展
- 2025年云南省卫生健康系统事业单位招聘基础知识类精练题(附答案)
- 酒店评优方案
- 企业战略管理试题及答案 12套试卷
- 法瑞西单抗注射液-药品临床应用解读
- 食堂原材料采购管理方案及食品保存管理方案
- 普惠金融趋势下的商业银行数字化转型发展探究
- 2025年高级考评员职业技能等级认定考试题(附答案)
- 沧州市盐山县2024-2025学年五年级数学第二学期期末复习检测试题含答案
- 2024年五年级英语下册 Module 3 Unit 2 Sam ate four hamburgers说课稿 外研版(三起)
- 保险行业大数据分析与精准客户画像方案
- 酒店前台收银员聘用合同
评论
0/150
提交评论