2021-2학기/C++
프렌드와 연산자 중복
애플쩀
2021. 11. 12. 14:37
전위연산자는 참조매개변수를 써서 값을 바로 바꿔준다
#include <iostream>
using namespace std;
#include <string>
class Power {
int kick;
int punch;
public:
Power(int kick = 0, int punch = 0) {
this->kick = kick;
this->punch = punch;
}
void show();
friend Power& operator++(Power& op);
friend Power operator++(Power& op, int x);
};
void Power::show() {
cout << "kick" << kick << ", punch" << punch << endl;
}
Power& operator++(Power& op) {
op.kick++;
op.punch++;
return op;
}
Power operator++(Power& op, int x) {
Power tmp = op;
op.kick++;
op.punch++;
return tmp;
}
int main() {
Power a(3,5), b;
b = ++a;
a.show();
b.show();
b = a++;
a.show(); b.show();
}