运算符重载程序例题解答

上传人:mg****85 文档编号:34152646 上传时间:2018-02-21 格式:DOCX 页数:7 大小:14.46KB
返回 下载 相关 举报
运算符重载程序例题解答_第1页
第1页 / 共7页
运算符重载程序例题解答_第2页
第2页 / 共7页
运算符重载程序例题解答_第3页
第3页 / 共7页
运算符重载程序例题解答_第4页
第4页 / 共7页
运算符重载程序例题解答_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《运算符重载程序例题解答》由会员分享,可在线阅读,更多相关《运算符重载程序例题解答(7页珍藏版)》请在金锄头文库上搜索。

1、/*1.定义一个复数类,通过重载运算符:+ 、- 、* 、/ 等,实现两个复数之间的各种运算。编写一个完整的程序。*/#includeclass Complexfloat Real,Image;public:Complex(float x=0,float y=0)Real=x;Image=y;friend Complex operator + (Complex friend Complex operator - (Complex friend Complex operator * (Complex friend Complex operator / (Complex void show()co

2、utclass pointint x;int y;int z;public:point(int X=0, int Y=0,int Z=0)x=X;y=Y;z=Z;point operator +(point &a)point t;t.x=x+a.x;t.y=y+a.y;t.z=z+a.z;return t;friend point operator +(point friend point operator +(point friend point operator -(point friend point operator -(point void show()cout#includecla

3、ss strint length;char *p;public:str()length=0;p=0;str(char *s) if(s)length=strlen(s)+1;p=new char length;strcpy(p,s);else length=0;p=0;str(str &s1)length=s1.length +1;p=new char length;strcpy(p,s1.p);str()if(p) delete p;friend str operator +(str friend str operator -(str void operator =(str void sho

4、w()cout, 用于两个字符窜的等于、小于和大于的比较运算。*/#include#includeclass strchar *p;public:str()p=0;str(char *s)p=s;friend bool operator (str friend bool operator (str &a,str &b)if(strcmp(a.p,b.p)0) return 1;else return 0;bool operator B) A.show();else B.show();if(AC) A.show();else C.show();if(B=C) B.show();else C.show();if(A=B) A.show();else B.show();

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

当前位置:首页 > 生活休闲 > 科普知识

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