반응형

문제

문제 7번의 Circle 객체에 대해 다음 연산이 가능하도록 연산자를 구현하라. 

int main()
{
	Circle a(5), b(4);
	b = 1 + a;
	a.show();
	b.show();
}

 

결과

radius = 5 인 원

radius = 6 인 원

 

소스코드(friend 함수 구현)

#include<iostream>
using namespace std;
class Circle {
	int radius;
public:
	Circle(int radius = 0) { this->radius = radius; }
	void show() { cout << "radius = " << radius << " 인 원" << endl; }
	friend Circle operator+(int n,Circle a);
};
Circle operator+(int n, Circle a)
{
	a.radius += n;
	return a;
}

int main()
{
	Circle a(5), b(4);
	b = 1 + a;
	a.show();
	b.show();
}

 

 

반응형

+ Recent posts