C++ 過載運算子
阿新 • • 發佈:2019-05-16
#include <iostream>
using namespace std;
class Point{
private :
int x;
public :
Point(int x1){
x=x1;
}
Point(Point & p){
x=p.x;
}
int getX();
const Point operator+(const Point & p);
friend const Point operator-(const Point & p1 ,const Point & p2);//宣告一個友元
};
int Point ::getX(){
return x;
}
const Point Point::operator+(const Point & p){
return Point(x+p.x);
}
Point const operator-(const Point & p1 ,const Point & p2){
return Point(p1.x-p2.x);
}
void main(){
Point a(1);
Point b(3);
Point c(0);
c=a+b;
cout<<c.getX()<<endl;
Point d(0);
d=a-b;
cout<<d.getX()<<endl;
}