C程序设计实验

上传人:工**** 文档编号:498439654 上传时间:2022-09-09 格式:DOC 页数:7 大小:33KB
返回 下载 相关 举报
C程序设计实验_第1页
第1页 / 共7页
C程序设计实验_第2页
第2页 / 共7页
C程序设计实验_第3页
第3页 / 共7页
C程序设计实验_第4页
第4页 / 共7页
C程序设计实验_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《C程序设计实验》由会员分享,可在线阅读,更多相关《C程序设计实验(7页珍藏版)》请在金锄头文库上搜索。

1、-实验6运算符重载实验目的l 掌握运算符重载的规则;l 掌握运算符成员函数与运算符友元函数的实现及应用;l 学会定义类中单目和双目运算符的重载函数;l 理解重载运算符的作用,学会对典型的运算符进展重载。实验学时本次实验需要2个学时。实验要求l 实验上机之前,根据实验容要求,自行设计编写程序,完成预习报告。l 实验上机时调试并修正程序。l 当次上机完毕前分析错误原因并给出实验结论,提交实验报告。实验容1.根底局部1定义复数类ple*,包括私有数据成员实部real和虚部image。定义该类的构造,拷贝构造,析构函数。为该类重载运算符+,-友元函数,前置和后置+,-成员函数,插入符和提取符友元函数。

2、在main函数里定义复数对象,测试重载的这些运算符。2.进阶局部2设计一个mystring类,包括数据成员char * pstr; 和 int length; 通过运算符重载实现字符串的输入、输出、下标等运算。/*1定义复数类ple*,包括私有数据成员实部real和虚部image。定义该类的构造,拷贝构造,析构函数。为该类重载运算符+,-友元函数,前置和后置+,-成员函数,插入符和提取符友元函数。在main函数里定义复数对象,测试重载的这些运算符。#include#includeusing namespace std;class ple* public:ple*(int real1=0,int

3、 image1=0) :real(real1),image(image1)ple*() ;friend ple* operator+(const ple* &a1, const ple* &a2);friend ple* operator-(const ple* &a1, const ple* &a2);ple* operator+();ple* operator+(int);ple* operator-();ple* operator-(int);friend ostream& operator(istream& is, ple*&a3);private:int real;int image

4、;ple* operator+(const ple* &a1, const ple* &a2)return ple*(a1.real + a2.real, a1.image + a2.image);ple* operator-(const ple* &a1, const ple* &a2)return ple*(a1.real - a2.real, a1.image - a2.image);ple* ple*:operator+()+real;+image;return *this;ple* ple*:operator+(int)ple* a = *this;+(*this);return a

5、;ple* ple*:operator-()-real;-image;return *this;ple* ple*:operator-(int)ple* a = *this;-(*this);return *this;ostream& operator(ostream& os, const ple*& a3)os a3.real + a3.image (istream& is, ple*&a3)is a3.real a3.image;return is;int main()ple* a4(4,5), a5(6,7),A,B;cout a4: a4 endl;cout a5: a5 endl;c

6、out a4;cin a5;cout 重新输入后a4: a4 endl;cout 重新输入后a5: a5 endl;A = a4 + a5;cout 重载修改后加法A:;cout A endl;A = a4 - a5;cout 重载修改后减法A:;cout A endl;cout 重载修改后a4前置+:+a4 endl;cout 重载修改后a5后置+: a5+ endl;cout 重载修改后再次修改的a4前置-: -a4 endl;cout 重载修改后再次修改的a5后置-: a5- 、输出 、下标等运算。#include #include using namespace std;class m

7、ystringpublic:mystring(char *p);mystring()free(pstr);mystring& operator=(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-pst

8、r, 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 pstr, s.pstr)(const mystring & s)if (strcmp(this-pstr, s.pstr)0)return 1;elsereturn 0;friend ostream & operator (ostream & os, const mystring & s) return os

9、 s.pstr (istream & is, mystring & s)delete s.pstr;is s.length ;s.pstr = new chars.length + 1;is s.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:

10、operator =(mystring & s)deletepstr; this-pstr = new charstrlen(s.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);/cout c;c

11、out a b endl;a = b;cout a endl;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 endl;实验心得:本次实验我们所学习的是函数的重载,函数的重载是为了满足不同环境下调用函数不同的问题。比方求最大值,我们三个值求最大值,两个值求更大值。在这里运用函数的重载我只需要定义一个函数为ma*,采用不同个数的形参就可以完成,这样使读者更加的清晰你要在这个函数中所表达的意思。通过本次实验,我也着实感受到了一次编程的乐趣,从中也学到了很多知识,如今C+的编程中我们所使用的语言更加的精简,没有C中则多的繁琐,这次的实验也相对来说比拟根底,知识为我么以后C+学习做好一个引子的作用,我相信下次的实验一定会是更加精彩的,能够让我有更多的体会,在C+中寻找到更大的乐趣和编程的动力。. z.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 研究生课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号