《运算符重载程序例题解答》由会员分享,可在线阅读,更多相关《运算符重载程序例题解答(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();