C++ 之 运算符重载

来源:互联网 发布:知乎 日本留学 编辑:程序博客网 时间:2024/06/05 09:21

 

#include <iostream>using namespace std;class point{private:int a,b;public:point(int a, int b){ //构造方法this->a = a;this->b = b;}int getA(){return this->a;}int getB(){return this->b;}void add(int a, int b){ //点相加this->a += a;this->b += b;printf("a+b=(%d, %d)\n",this->a, this->b);}void add(point p){add(p.getA(), p.getB());}void operator += (point p){ //运算符重载this->add(p.getA(), p.getB());}};int main(){point * p = new point(5, 5);//p->add(2,2);(*p) += point(2, 2);//此处可以直接使用 += 来进行类的运算system("pause");return 0;}


 

0 0
原创粉丝点击