
구조체 내에 함수 정의하기
문제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
반응형
'etc. > 열혈 C++ 프로그래밍' 카테고리의 다른 글
(열혈 C++ 프로그래밍) 문제 04-1 (0) | 2021.11.15 |
---|---|
(열혈 C++ 프로그래밍) 문제 03-2 (2) | 2021.11.11 |
(열혈 C++ 프로그래밍) 문제 02-4 (0) | 2021.11.11 |
(열혈 C++ 프로그래밍) 문제 02-3 (0) | 2021.11.11 |
(열혈 C++ 프로그래밍) 문제 02-2 (0) | 2021.11.11 |

구조체 내에 함수 정의하기
문제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
반응형
'etc. > 열혈 C++ 프로그래밍' 카테고리의 다른 글
(열혈 C++ 프로그래밍) 문제 04-1 (0) | 2021.11.15 |
---|---|
(열혈 C++ 프로그래밍) 문제 03-2 (2) | 2021.11.11 |
(열혈 C++ 프로그래밍) 문제 02-4 (0) | 2021.11.11 |
(열혈 C++ 프로그래밍) 문제 02-3 (0) | 2021.11.11 |
(열혈 C++ 프로그래밍) 문제 02-2 (0) | 2021.11.11 |