[Absolute C++]Ch6-3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
using namespace std;

class Point {

public:
 Point(); //디폴트 생성자
 void set(); // 초기 좌표 값을 설정하는 멤버함수
 void moveTo(); // 좌표 이동 멤버함수
 void Rotate90(); // 원점 중심 90도 회전변환 멤버함수
 void printPos(); // 현재 좌표를 출력해주는 멤버함수 


private:
 int x; // 멤버변수, 좌표를 나타내는 x값과 y값
 int y;
};


int main() {

 Point point1; // 클래스 변수 선언시 인자가 없는 생성자를 호출할 때에는 괄호를 사용하지 않는다.
 cout << "시작\n"<<endl;
 char flag = 'y';
 point1.set(); // 초기 값 설정
 while (flag == 'y' || flag == 'Y') { // 반복문을 통해 좌표 이동
  point1.moveTo();
  point1.Rotate90();
  point1.printPos();
  cout << "Continue?(Enter 'Y' or 'y' to continue) : ";
  cin >> flag;
 }
}

Point::Point()
 :x(0), y(0) {} //초기화 섹션 - 초기화 값으로 생성자 매개변수를 사용할 수 있다.

void Point::set() {
 
 cout << "초기x값 입력: ";
 cin >> x;
 cout << "초기y값 입력: ";
 cin >> y;

}

void Point::Rotate90() { //90도 회전변환 cos(-pi/2) = 0, sin(-pi/2) = -1 => newX = +y , newY = -x
 int temp;
 temp = x;
 x = y;
 y = temp * (-1);

 cout << "원점을 중심으로 90도 회전변환합니다." << endl;
}

void Point::moveTo() {
 int xValue, yValue;
 
 cout << "수평방향(x축)으로 얼마나 이동하시겠습니까? : ";
 cin >> xValue;
 
 cout << "수직방향(y축)으로 얼마나 이동하시겠습니까? : ";
 cin >> yValue;

 x += xValue;
 y += yValue;

}

void Point::printPos() {
 cout << "x좌표의 값은 " << x << "입니다." << endl;
 cout << "y좌표의 값은 " << y << "입니다." << endl;
}

댓글