自己敲的代码,如果有错误,欢迎指出,所有运算符重载的代码都放在主体代码里面便可跑起来
一元运算符重载
主体代码
#include<iostream> using namespace std; class point { int x,y,z; public: point(){} point (int X,int Y,int Z):x(X),y(Y),z(Z){} void show(){ cout<<"x="<<x<<' '<<"y="<<y<<' '<<"z="<<z<<endl; } }; int main(){ int a,b,c; cin>>a>>b>>c; point p1(a,b,c); point p2; p2=-p1; p2.show();//x=-1 y=-2 z=-3 p2=p1++; p2.show();//x=1 y=2 z=3 p2=p1--; p2.show();//x=2 y=3 z=4 p2=++p1; p2.show();//x=2 y=3 z=4 p2=--p1; p2.show();//x=1 y=2 z=3 return 0; }
自增(++)自减(--)运算符
friend point operator ++(point&a){ a.x++; a.y++; a.z++; return a; } friend point operator ++(point&a,int ){ point t; t.x=a.x++; t.y=a.y++; t.z=a.z++; return t; } friend point operator --(point&a){ a.x--; a.y--; a.z--; return a; } friend point operator --(point&a,int ){ point t; t.x=a.x--; t.y=a.y--; t.z=a.z--; return t; }
一元减运算符,即负号( - )
friend point operator -(point&a ){ point t; t.x=-a.x; t.y=-a.y; t.z=-a.z; return t; } /* point & operator -(){ x=-x; y=-y; z=-z; return *this; } */
逻辑非运算符( ! )
二元运算符重载
主体
#include<iostream> using namespace std; class point { int x,y,z; public: point (int X=0,int Y=0,int Z=0){x=X;y=Y;z=Z;} void show(){ cout<<"x="<<x<<' '<<"y="<<y<<' '<<"z="<<z<<endl; } }; int main(){ int a,b,c; cin>>a>>b>>c; point p1(a,b,c); cin>>a>>b>>c; point p2(a,b,c); p2=p1+p2; p2.show(); return 0; }
加运算符( + )
friend point operator +(point&a,point &b){ point t; t.x=a.x+b.x; t.y=a.y+b.y; t.z=a.z+b.z; return t; }
减运算符( - )
乘运算符( * )
除运算符( / )
关系运算符(用bool)
主体代码
#include<iostream> using namespace std; class point { int x,y,z; public: point (int X=0,int Y=0,int Z=0){x=X;y=Y;z=Z;} void show(){ cout<<"x="<<x<<' '<<"y="<<y<<' '<<"z="<<z<<endl; } }; int main(){ int a,b,c; cin>>a>>b>>c; point p1(a,b,c); cin>>a>>b>>c; point p2(a,b,c); if(p1>p2){ cout<<"点1与原点的距离大于点2"; } else{ cout<<"点1与原点的距离小于等于点2"; } return 0; }
>
friend bool operator >(const point &p1,const point &p2){ return (p1.x*p1.x+p1.y*p1.y+p1.z*p1.z) >(p2.x*p2.x+p2.y*p2.y+p2.z*p2.z); }
<
<=
>=
==
输入/输出运算符重载
主体代码
#include<iostream> #include<cmath> using namespace std; class point { int x,y,z; public: point (int X=0,int Y=0,int Z=0){x=X;y=Y;z=Z;} void show(){ cout<<"x="<<x<<' ' <<"y="<<y<<' ' <<"z="<<z<<endl; } }; int main(){ int a,b,c; //输入方法1(简单) cin>>a>>b>>c; point p1(a,b,c); //输入方法2 point p2; cin>>p2; //输出方法1 cout<<p1; cout<<p2; //输出方法2(简单) p1.show(); p2.show(); }
流提取运算符 >>
friend istream &operator >>(istream &in,point &p){ in>>p.x>>p.y>>p.z; return in; }
流插入运算符 <<
friend ostream &operator<<( ostream &out,const point&p){ out<<"x="<<p.x<<" "<<"y="<< p.y<<" "<<"z="<<p.z<<endl; return out; }
输入 如下: 输出如下:
![]()
重载赋值运算符
#include<iostream> #include<cmath> using namespace std; class point { int x,y,z; public: point (int X=0,int Y=0,int Z=0){x=X;y=Y;z=Z;} //代码插入这里// }; int main(){ int a,b,c; //输入方法1(简单) cin>>a>>b>>c; point p1(a,b,c); cout<<"与原点的距离"<<(double)p1; }
=
运算符 () 和[]
主体代码
#include<iostream> #include<cmath> using namespace std; class point { int x,y,z; public: point (int X=0,int Y=0,int Z=0){x=X;y=Y;z=Z;} //代码插入这里// }; int main(){ int a,b,c; //输入方法1(简单) cin>>a>>b>>c; point p1(a,b,c); cout<<"与原点的距离"<<(double)p1; }
重载类型转换()
operator double (){ return sqrt(x*x+y*y+z*z); }
输入 输出
![]()
重载运算符()和下标操作符 [] 比较
#include<iostream> using namespace std; class point { int *p; public: point (int a[]){ p=new int [3]; for(int i=0;i<3;i++){ p[i]=a[i]; } } }; int main(){ int a[3]; for(int i=0;i<3;i++){ cin>>a[i]; } point p1(a); cout<<"输出a[1]:"<<a[1] <<"是和输出p1(1):"<<p1(1)<<"一样的"<<endl; cout<<"输出a[1]:"<<a[1] <<"是和输出p1[1]:"<<p1[1]<<"一样的"; return 0; }
int & operator [] (int i ){ return p[i] ; } int & operator ()(int i){ return p[i]; }
输入 输出
![]()