Jam's story
프렌드와 연산자 중복 본문
전위연산자는 참조매개변수를 써서 값을 바로 바꿔준다
#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();
}
'2021-2학기 > C++' 카테고리의 다른 글
C++ programming 8장 실습문제 (0) | 2021.11.18 |
---|---|
명품 c++programming 7장 실습문제 (0) | 2021.11.13 |
4장 (0) | 2021.10.26 |
3장 실습문제 (0) | 2021.10.26 |
3단원 (0) | 2021.10.26 |