friend
๋๊ฐ์ ํด๋์ค๊ฐ ์์ ๋, ์๋ก ๋ง๋ค์ด์ง ๋ชจ๋ ๋ฉค๋ฒ๋ณ์, ๋ฉค๋ฒํจ์๋ฅผ ๊ณต์ ํ๊ธฐ ์ํด์ ์ฐ๋ฆฌ๋ friend ๋ผ๋ ํค์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
a ๊ฐ์ฒด๊ฐ b ๊ฐ์ฒด๋ฅผ ์น๊ตฌ๋ก ์ ์ธํ๋ค๋ฉด b๊ฐ์ฒด๋ a๊ฐ์ฒด์ ๋ชจ๋ ๋ณ์์ ํจ์๊ฐ์ ๊ฐ๋ค๊ฐ ์ฌ์ฉํ ์ ์๋ค.
privateย ๊น์ง ์ฌ์ฉ ๊ฐ๋ฅํ๋ค!!!!
#include <iostream>
using namespace std;
class Point{
private:
int x;
int y;
public:
Point(): x(0), y(0){}
Point(int _x, int _y): x(_x), y(_y) {}
void setXY(int _x, int _y){
this->x = _x;
this->y = _y;
}
int getX() const { return this->x; }
int getY() const { return this->y; }
Point operator + (Point& pt){
Point result(this->x + pt.x, this->y + pt.y);
return result;
}
Point operator - (Point& pt){
Point result(this->x - pt.x, this->y - pt.y);
return result;
}
Point operator = (Point& pt){
this->x = pt.x;
this->y = pt.y;
return (*this);
}
// SpyPoint์ Point ํด๋์ค์ ๋ชจ๋ ์ ๋ณด๋ฅผ ๋๊ฒจ์ค๋ค.
friend class SpyPoint;
};
class SpyPoint{
public:
void print_all_point_info(Point& pt){
cout << "SpyPoint์ ์ํด ์ถ๋ ฅ๋ฉ๋๋ค." << endl;
cout << "x : " << pt.x << endl;
cout << "y : " << pt.y << endl;
}
};
int main(){
Point pt1(1, 2), pt2(3, 4);
SpyPoint spyPt;
spyPt.print_all_point_info(pt1);
spyPt.print_all_point_info(pt2);
return 0;
}
Output
SpyPoint์ ์ํด ์ถ๋ ฅ๋ฉ๋๋ค.
x : 1
y : 2
SpyPoint์ ์ํด ์ถ๋ ฅ๋ฉ๋๋ค.
x : 3
y : 4
Program ended with exit code: 0