[C++] 움직이는 자동차 시뮬레이션 (구조체, 벡터, 가상함수)
2022. 6. 15. 22:54ㆍ코딩 1막 <C++개념편>/코딩 1막 <C++응용편>
728x90
#include <iostream>
#include <vector>
#include <string>
#define Enter "\n\n\n\n";
using namespace std;
struct Car
{
string name;
string carNumber;
int oil;
bool start = false;
int distanceDriven = 0;
Car() {}
Car(string _name, string _carNumber, int _oil, int _distanceDriven = 0)
{
name = _name;
carNumber = _carNumber;
oil = _oil;
distanceDriven = _distanceDriven;
}
virtual ~Car()
{
cout << "기본차 소멸자" << endl;
}
void virtual PrintInfo()
{
cout << "==========기본 차 정보==========" << endl;
cout << "차 이름 : " << name << endl;
cout << "차 번호 : " << carNumber << endl;
cout << "기름 상태 : " << oil << "%" << endl;
cout << "주행 거리 : " << distanceDriven << "km" << endl;
if (start) cout << "시동 : 켜져 있음" << endl;
else cout << "시동 : 꺼져 있음" << endl;
cout << endl;
}
void ChargeOil(int _oil)
{
if (oil == 100)
cout << "기름이 가득 차 있습니다." << endl << endl;
else if (oil + _oil > 100) // 100을 넘어간다면
{
cout << "기름을 더 넣을 수 없습니다." << endl;
cout << "기름 상태 : " << oil << "%" << endl;
cout << endl;
}
else
{
oil += _oil;
cout << "기름을 " << _oil << "만큼 충전했습니다." << endl;
cout << "기름 상태 : " << oil << "%" << endl;
cout << endl;
}
}
void StartCar()
{
if (!oil)
{
cout << "기름이 없습니다. 기름을 충전해주세요 ! " << endl;
start = false;
cout << endl;
}
else
{
cout << name << " 차가 1km 이동했습니다." << endl;
distanceDriven++;
oil--;
cout << "주행 거리 : " << distanceDriven << "km" << endl;
cout << "기름 상태 : " << oil << "%" << endl;
cout << endl;
}
}
void StartCar(int distance)
{
if (!oil)
{
cout << "기름이 없습니다. 기름을 충전해주세요 ! " << endl;
cout << endl;
}
else if (oil - distance < 0)
{
distanceDriven += oil;
cout << "기름이 부족해 " << oil << "만큼 이동했습니다." << endl;
cout << "주행 거리 : " << distanceDriven << "km" << endl;
cout << "기름 상태 : " << oil << "%" << endl;
cout << "기름을 충전해주세요 ! " << endl;
cout << endl;
}
else
{
oil -= distance;
distanceDriven += distance;
cout << name << " 차가 " << distance << "km 만큼 이동했습니다." << endl;
cout << "기름 상태 : " << oil << "%" << endl;
cout << "주행 거리 : " << distanceDriven << "km" << endl;
cout << endl;
}
}
};
struct SmallCar : Car
{
string carType;
SmallCar() {}
SmallCar(string _name, string _carNumber, int _oil, int _distanceDriven, string _carType) : Car(_name, _carNumber, _oil, _distanceDriven = 0)
{
carType = _carType;
}
~SmallCar()
{
cout << "소형차 소멸자" << endl;
}
void PrintInfo()
{
cout << "==========소형차 정보==========" << endl;
cout << "차 이름 : " << name << endl;
cout << "차 번호 : " << carNumber << endl;
cout << "기름 상태 : " << oil << "%" << endl;
cout << "주행 거리 : " << distanceDriven << "km" << endl;
if (start) cout << "시동 : 켜져 있음" << endl;
else cout << "시동 : 꺼져 있음" << endl;
cout << endl;
}
};
struct LargeCar : Car
{
string carType;
LargeCar() {}
LargeCar(string _name, string _carNumber, int _oil, int _distanceDriven, string _carType) : Car(_name, _carNumber, _oil, _distanceDriven = 0)
{
carType = _carType;
}
~LargeCar()
{
cout << "대형차 소멸자" << endl;
}
void PrintInfo()
{
cout << "==========대형차 정보==========" << endl;
cout << "차 이름 : " << name << endl;
cout << "차 번호 : " << carNumber << endl;
cout << "기름 상태 : " << oil << "%" << endl;
cout << "주행 거리 : " << distanceDriven << "km" << endl;
if (start) cout << "시동 : 켜져 있음" << endl;
else cout << "시동 : 꺼져 있음" << endl;
cout << endl;
}
};
void main()
{
//============================ virtual 동적배열 실습 ============================//
/*
SmallCar smallCarA("모닝", "굿모닝", 20, 0, "smallCar");
SmallCar smallCarB("중고모닝", "맥모닝", 0, 0, "smallCar");
LargeCar largeCarA("봉고", "그런가봉고", 0, 0, "largeCar");
LargeCar largeCarB("봉고II", "방가", 0, 0, "largeCar");
vector<Car> carVec;
vector<Car*> carPtrVec;
carVec.push_back(smallCarA);
carVec.push_back(smallCarB);
carVec.push_back(largeCarA);
carVec.push_back(largeCarB);
carPtrVec.push_back(&smallCarA);
carPtrVec.push_back(&smallCarB);
carPtrVec.push_back(&largeCarA);
carPtrVec.push_back(&largeCarB);
for (int i = 0; i < carVec.size(); i++)
carVec[i].PrintInfo();
cout << "\n\n\n";
for (int i = 0; i < carPtrVec.size(); i++)
carPtrVec[i]->PrintInfo();
for (int i = 0; i < carPtrVec.size(); i++)
delete carPtrVec[i];
*/
//===============================================================================//
//============================ new 동적배열 ============================//
/*
vector<Car*> carPtrVec;
carPtrVec.push_back(new SmallCar("모닝", "굿모닝", 20, 0, "smallCar"));
carPtrVec.push_back(new SmallCar("중고모닝", "맥모닝", 0, 0, "smallCar"));
carPtrVec.push_back(new LargeCar("봉고", "그런가봉고", 0, 0, "LargeCar"));
carPtrVec.push_back(new LargeCar("봉고II", "방가", 0, 0, "LargeCar"));
for (int i = 0; i < carPtrVec.size(); i++)
carPtrVec[i]->PrintInfo();
for (int i = 0; i < carPtrVec.size(); i++)
delete carPtrVec[i];
*/
//======================================================================//
Car carA("모닝", "굿모닝", 0);
Car carB("중고모닝", "맥모닝", 20);
Car carC("아반떼", "12가3456", 0);
vector<Car> carVec;
carVec.push_back(carA);
carVec.push_back(carB);
carVec.push_back(carC);
int carChoice;
int choice;
int inputDistance;
int inputChargeOil;
cout << "당신의 차를 선택해주세요" << endl;
for (int i = 0; i < carVec.size(); i++)
cout << i + 1 << "번 : " << carVec[i].name << endl;
cin >> carChoice;
carChoice -= 1;
cout << "당신의 차 정보는 다음과 같습니다." << endl;
carVec[carChoice].PrintInfo();
while (1)
{
cout << "행동을 선택해주세요 (1. 1km 이동 2. 장거리 이동 3. 기름 충전 4. 차 정보 확인 5. 종료) : ";
cin >> choice;
cout << "-----------------------------------------------------" << endl;
if (choice == 1)
{
carVec[carChoice].StartCar();
Enter
}
else if (choice == 2)
{
cout << "이동할 거리를 입력해주세요 : ";
cin >> inputDistance;
carVec[carChoice].StartCar(inputDistance);
Enter
}
else if (choice == 3)
{
cout << "충전할 기름 %를 입력해주세요 (최대 100%) : ";
cin >> inputChargeOil;
carVec[carChoice].ChargeOil(inputChargeOil);
Enter
}
else if (choice == 4)
{
carVec[carChoice].PrintInfo();
Enter
}
else if (choice == 5)
break;
cout << "-----------------------------------------------------" << endl;
}
}
728x90
'코딩 1막 <C++개념편> > 코딩 1막 <C++응용편>' 카테고리의 다른 글
[C++] 게임 오프닝 만들기 (0) | 2022.06.16 |
---|---|
[C++] 상호참조를 할만한 두 구조체를 만들고 선언부와 구현부를 분리해보자 (0) | 2022.06.16 |
[C++] 운송수단을 객체로 구조체를 짜서 상속 및 벡터와 가상함수, 동적할당 쓰기 (0) | 2022.06.15 |
[C++] 벡터를 사용해서 인벤토리 생성 (0) | 2022.06.14 |
[C++] 벡터를 사용해서 출석부만들기 (0) | 2022.06.14 |