내일배움캠프-언리얼

[내일배움캠프 - 언리얼] DAY 11 - 클래스로 자동차 만들기

이시보 2025. 12. 11. 20:55
#include <iostream>
#include <string>

using namespace std;

// 자동차 클래스
class Car
{
public:
	string modelName; // 모델 이름
	float speed; // 자동차 속도 변수

	Car(string modelName) // 매개변수로 모델 이름을 받는 생성자
	{
		Car::modelName = modelName;
		speed = 0; // 초기 속도 0
	}

	void Accelerate() // 가속 함수
	{
		speed += 10;
	}

	void Brake() // 브레이크 함수
	{
		if (speed >= 10)
			speed -= 10;
		else
			speed = 0;
	}
	
	void DisplayInfo() // 이름과 속도를 출력하는 함수
	{
		cout << "[자동차명 : " << modelName << " 현재 속도 : " << speed << "]" << endl;
	}
};

int main()
{
	Car Ferrari("Ferrari"); // 페라리 이름의 자동차 생성
	int input;
	bool Riding = true; // 현재 달리는 중인지 확인하는 부울 변수

	while (Riding)
	{
		Ferrari.DisplayInfo();
		cout << "1) 가속" << endl << "2) 브레이크" << endl << "3) 종료" << endl;
		cin >> input;
		switch (input)
		{
			case 1:
			{
				Ferrari.Accelerate();
				break;
			}
			case 2:
			{
				Ferrari.Brake();
				break;
			}
			case 3:
			{
				Riding = false;
				break;
			}
			default:
			{
				cout << "잘못된 번호입니다. 다시 입력해주세요." << endl;
				break;
			}
		}
	}

	return 0;
}