Code

[Absolute C++] Ch6-3

BIGFROG 2019. 9. 19. 11:17
#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;
}

'Code' 카테고리의 다른 글

[Absolute C++] Ch2-3  (0) 2019.09.19
[Absolute C++] Ch7-4  (0) 2019.09.19
[Absolute C++] Ch10-2  (0) 2019.09.19
[Absolute C++] Ch14-9  (0) 2019.09.19
연결리스트(Linked List)  (0) 2019.07.17