참조 : 윤성우의 열혈 C++
클래스 Car 를 대상으로 파일을 나눠보자.
namespace CAR_CONST
{
enum
{
ID_LEN = 20,
MAX_SPD = 200,
FUEL_STEP = 2,
ACC_STEP = 10,
BRK_STEP = 10
};
}
class Car
{
private:
char gamerID[CAR_CONST::ID_LEN];
int fuelGage;
int curSpeed;
public:
void InitMembers(char *ID, int fuel);
void ShowCarState();
void Accel();
void Break();
};
#incldue <iostream>
#include <cstring>
#include "Car.h"
using namespace std;
void Car::InitMembers(char *ID, int fuel)
{
strcpy(gamerID, ID);
fuelGage = fuel;
curSpeed = 0;
}
void Car::ShowCarState()
{
cout << "사용자 ID: " << gamerID << endl;
cout << "연료량: " << fuelGage << "%" << endl;
cout << "현재속도: " << curSpeed << "km/s" << endl;
}
void Car::Accel()
{
if (fuelGage <= 0)
return;
else
fuelGage -= CAR_CONST ::FUEL_STEP;
if ((curSpeed += CAR_CONST ::ACC_STEP) >= CAR_CONST ::MAX_SPD)
{
curSpeed = CAR_CONST ::MAX_SPD;
return;
}
curSpeed += CAR_CONST ::ACC_STEP;
}
main.cpp
#include "Car.h"
int main(void)
{
Car run99;
run99.InitMembers("run99", 100);
run99.Accel();
run99.Accel();
run99.Accel();
run99.ShowCarState();
run99.Break();
run99.ShowCarState();
return 0;
}