etc./열혈 C++ 프로그래밍

(열혈 C++ 프로그래밍) 문제 03-1

innit 2021. 11. 11. 15:57

구조체 내에 함수 정의하기

 

 

 

 

 

 

 

문제1

#include <iostream>
using namespace std;

struct Point
{
	int xpos;
	int ypos;

	void MovePos(int x, int y)
	{
		xpos += x;
		ypos += y;
	}

	void AddPoint(const Point& pos)
	{
		xpos += pos.xpos;
		ypos += pos.ypos;
	}

	void ShowPosition()
	{
		cout << "[" << xpos << " ," << ypos << "]" << endl;
	}
};

int main(void)
{
	Point pos1 = { 12, 4 };
	Point pos2 = { 20, 30 };

	pos1.MovePos(-7, 10);
	pos1.ShowPosition();

	pos1.AddPoint(pos2);
	pos1.ShowPosition();
	
	return 0;
}

<추가설명>

C++은 C와 다르게 구조체 안에 함수를 선언하여 사용할 수 있습니다.

 

 

 

 

728x90
반응형