교환법칙
교환법칙
A+B 는 B+A와 결과가 같다.
#include <iostream>
using namespace std;
class Point {
int xPos;
int yPos;
public:
Point() {}
Point(int x, int y)
: xPos(x), yPos(y) {}
void showPosition() const {
cout << "[" << xPos << ", " << yPos << "]" << endl;
}
Point operator*(int num) {
Point pos(xPos*num, yPos*num);
return pos;
}
friend Point operator*(int num, Point& ref);
};
Point operator*(int num, Point& ref) {
return ref * num;
}
int main() {
Point p1(3, 5);
Point p2 = p1 * 10;
Point p3 = 5 * p1;
p1.showPosition();
p2.showPosition();
p3.showPosition();
return 0;
}Last updated