본문 바로가기

강의자료/C/C++

043. 클래스의 객체를 직접적으로 입출력 시켜보자.


◈ 이 글은 강의를 위하여 제가 직접 작성한 내용입니다. 따라서 퍼가실 경우, 출처를 명확히 해주시기 바랍니다!!
◈ This ariticle was written by me for teaching. So if you want to copy this article, please clarify the source!!


클래스의 객체에 있는 값을 출력하기 위해서 cout에 대입해보라.
당연히 출력이 되지 않을 것이다. 왜냐하면 객체에는 많은 데이터가 들어있고 이 중에 어떤 데이터를 출력해야할지 cout이 모르기때문이다.
이 또한, 이전에 배웠던 연산자 오버로딩을 이용하여 구현할 수 있다.
이를 이용하면, cout 뿐만이 아니라 원하는 모든 입출력에 사용할 수 있다.

// 동등 연산자를 직접 만들어보자.
// 입출력 연산자 오버로딩에 대해서 이해한다.
// cout, cin을 이용한 입출력을 위해서는 friend를 이용해야만 한다.
/*
>> 연산자 오버로딩을 작성하는 또하나의 방법부터 이해한다.
   (외부 함수에서 연산자의 좌우 인자를 모두 정의하여 작성하면 된다.)
Point operator +(const Point& lhs, const Point& rhs)
{
	return Point(lhs.getX()+rhs.getX(), lhs.getY()+rhs.getY());      
}
*/
// friend에 대해서 이해한다.
/*
#include 
using namespace std;

class FRIEND
{
private:
	int x;
	int y;

public:
	FRIEND(int n1, int n2)
	{
		x = n1, y = n2;
	}

	//friend void Show(FRIEND& obj);
};

void Show(FRIEND& obj)
{
	cout << obj.x << ", " << obj.y << endl;
}

void main()
{
	FRIEND test(100, 200);
	Show(test);
}
*/

#include 
using namespace std;

class Point  
{
private:
	int x;
	int y;

public:
	Point(int _x, int _y);
	~Point();

	void position();
	void move(int _x, int _y);

	// 복사생성자.
	Point(const Point& p);  
	Point(const Point* p);

	int getX() const {return x;}  //값을 바꿀 수 없다. 
	int getY() const {return y;}  //값을 바꿀 수 없다. 

	// 연산자 오버로딩.
	Point():x(0), y(0) {}
	Point operator +(const Point& rhs) const;
	Point operator -(const Point& rhs) const;
	Point& operator =(const Point& rhs);

	// 새로 추가.
	bool operator ==(const Point &rhs) const
	{
		return (this->x==rhs.x && this->y==rhs.y);
	}

	friend ostream& operator << (ostream &output, const Point &rhs);
	friend istream& operator >> (istream &input, Point &rhs);
};

ostream& operator << (ostream &output, const Point &rhs)
{
	output << "(x, y)= (" << rhs.x << " , " << rhs.y << ")";
	return output;
}

istream& operator >> (istream &input, Point &rhs)
{
	input >> rhs.x >> rhs.y;
	return input;
}

Point::Point(int _x, int _y)
{
	x=_x;  
	y=_y;
}

Point::Point(const Point& p)
{
	x=p.getX();
	y=p.getY();                     
}
Point::Point(const Point* p)
{
	x=p->getX();
	y=p->getY();                   
}

Point::~Point()
{
	cout << "Point die~~." << endl;                
}

void Point::position()
{
	cout << "(" << x << " , " << y << ")" << endl;     
} 

void Point::move(int _x, int _y)
{
	x+=_x;  //_x만큼 이동  
	y+=_y;  //_y만큼 이동     
}

// 연산자 오버로딩.
Point Point::operator +(const Point& rhs) const
{
	return Point(x+rhs.getX(), y+rhs.getY());      
}
Point Point::operator -(const Point& rhs) const
{
	return Point(x-rhs.getX(), y-rhs.getY());  
}
Point& Point::operator=( const Point& rhs )
{
	if( this != &rhs )	
	{
		this->x = rhs.getX();
		this->y = rhs.getY();
	}
	return *this;
}

void main()
{
	Point p5(5,8);  //스택에 객체 생성 
	Point p6(2,7);   
	Point p7;       //기본 생성자로 생성
	p7=p5+p6;       //두점을 더한다.     
	Point p8=p5-p6;  
	cout << (p7==p8) << endl; //필드가 같다면 같은 객체 
	cout << p5 << endl;
	cout << p6 << endl;
	cout << p7 << endl;
	cout << p8 << endl;
	Point p9(2,1);
	cout << "x, y값을 입력하세요 : ";
	cin >> p9;
	cout << p9 << endl;
}