c++的运算符重载

c++的运算符重载_第1张图片
image.png

C++中的加号重载:
例:实现复数的相加

#include  
using namespace std;  
  
class Complex{  
    public:  
        Complex(double r=0.0,double i=0.0):real(r),imag(i){}  
        Complex operator+(const Complex &c2)const;  
        void display()const{  
            cout<<"("<
c++的运算符重载_第2张图片
image.png

C++中的前置++重载:
例:点的移动

#include  
using namespace std;  
  
class Point{  
    public:  
        Point(float a,float b):x(a),y(b){}  
        Point& operator++();  
        ~Point(){}  
        void dispaly()const{  
            cout<<"("<
c++的运算符重载_第3张图片
image.png

C++中的后置++重载:
例:点的移动

#include  
using namespace std;  
  
class Point{  
    public:  
        Point(float a,float b):x(a),y(b){}  
        Point operator++(int);  
        ~Point(){}  
        void dispaly()const{  
            cout<<"("<

你可能感兴趣的:(c++的运算符重载)