#include <iostream>
using namespace std;
#define ID_LEN 20
#define MAX_SPD 200
#define FUEL_STEP 2
#define ACC_STEP 10
#define BRK_STEP 10
struct Car
{
char gamerID[ID_LEN];
int fuelGauge;
int curSpeed;
};
void ShowCarState(const Car& car)
{
cout << "소유자ID: " << car.gamerID << endl;
cout << "연료량: " << car.fuelGauge << endl;
cout << "현재속도: " << car.curSpeed << "km/s" << endl << endl;
}
void Accel(Car& car)
{
if (car.fuelGauge <= 0)
return;
else
car.fuelGauge -= FUEL_STEP;
if (car.curSpeed + ACC_STEP >= MAX_SPD)
{
car.curSpeed = MAX_SPD;
return;
}
car.curSpeed += ACC_STEP;
}
void Break(Car& car)
{
if (car.curSpeed < BRK_STEP)
{
car.curSpeed = 0;
return;
}
car.curSpeed -= BRK_STEP;
}
int main()
{
Car run99 = { "run99",100,0 };
Accel(run99);
Accel(run99);
ShowCarState(run99);
Break(run99);
ShowCarState(run99);
Car sped77 = { "sped77",100,0 };
Accel(sped77);
Break(sped77);
ShowCarState(sped77);
return 0;
}
C언어는 구조체 안에 함수 삽입이 불가능하지만 C++에서는 가능하다.
#include <iostream>
using namespace std;
#define ID_LEN 20
#define MAX_SPD 200
#define FUEL_STEP 2
#define ACC_STEP 10
#define BRK_STEP 10
struct Car
{
char gamerID[ID_LEN];
int fuelGauge;
int curSpeed;
void ShowCarState(const Car& car)
{
cout << "소유자ID: " << car.gamerID << endl;
cout << "연료량: " << car.fuelGauge << endl;
cout << "현재속도: " << car.curSpeed << "km/s" << endl << endl;
}
void Accel(Car& car)
{
if (car.fuelGauge <= 0)
return;
else
car.fuelGauge -= FUEL_STEP;
if (car.curSpeed + ACC_STEP >= MAX_SPD)
{
car.curSpeed = MAX_SPD;
return;
}
car.curSpeed += ACC_STEP;
}
void Break(Car& car)
{
if (car.curSpeed < BRK_STEP)
{
car.curSpeed = 0;
return;
}
car.curSpeed -= BRK_STEP;
}
};
int main()
{
Car run99 = { "run99",100,0 };
run99.Accel(run99);
run99.Accel(run99);
run99.ShowCarState(run99);
run99.Break(run99);
run99.ShowCarState(run99);
Car sped77 = { "sped77",100,0 };
sped77.Accel(sped77);
sped77.Break(sped77);
sped77.ShowCarState(sped77);
return 0;
}
위의 코드 맨 위에 보면 매크로 상수들이 존재한다.
하지만 이 상수 값들은 구조체 Car에만 의미가 있는 상수이기 때문에 구조체 내부에 넣는것이 좋다.
이때 열거형 enum을 사용하여 구조체 내에서만 유효한 상수를 정의할 수 있다.
enum
#include <iostream> using namespace std; enum week { sun, // 0부터 초기화 mon = 3, tue, // 전에 숫자로부터 1씩 증가되어서 초기화 됨 wed }; int main() { printf("%d\n", sun); printf("%d\n", mon); printf("%d\n", tue); printf("%d\n", wed); return 0; }
#include <iostream>
using namespace std;
namespace CAR_CONST
{
enum
{
ID_LEN = 20,
MAX_SPD = 200,
FUEL_STEP = 2,
ACC_STEP = 10,
BRK_STEP = 10
};
}
struct Car
{
char gamerID[CAR_CONST::ID_LEN];
int fuelGauge;
int curSpeed;
void ShowCarState()
{
cout << "소유자ID: " << gamerID << endl;
cout << "연료량: " << fuelGauge << endl;
cout << "현재속도: " << curSpeed << "km/s" << endl << endl;
}
void Accel()
{
if (fuelGauge <= 0)
return;
else
fuelGauge -= 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;
}
void Break()
{
if (curSpeed < CAR_CONST::BRK_STEP)
{
curSpeed = 0;
return;
}
curSpeed -= CAR_CONST::BRK_STEP;
}
};
int main()
{
Car run99 = { "run99",100,0 };
run99.Accel();
run99.Accel();
run99.ShowCarState();
run99.Break();
run99.ShowCarState();
Car sped77 = { "sped77",100,0 };
sped77.Accel();
sped77.Break();
sped77.ShowCarState();
return 0;
}
#include <iostream>
using namespace std;
namespace CAR_CONST
{
enum
{
ID_LEN = 20,
MAX_SPD = 200,
FUEL_STEP = 2,
ACC_STEP = 10,
BRK_STEP = 10
};
}
struct Car
{
char gamerID[CAR_CONST::ID_LEN];
int fuelGauge;
int curSpeed;
void ShowCarState();
void Accel();
void Break();
};
void Car::ShowCarState()
{
cout << "소유자ID: " << gamerID << endl;
cout << "연료량: " << fuelGauge << endl;
cout << "현재속도: " << curSpeed << "km/s" << endl << endl;
}
void Car::Accel()
{
if (fuelGauge <= 0)
return;
else
fuelGauge -= 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;
}
void Car::Break()
{
if (curSpeed < CAR_CONST::BRK_STEP)
{
curSpeed = 0;
return;
}
curSpeed -= CAR_CONST::BRK_STEP;
}
int main()
{
Car run99 = { "run99",100,0 };
run99.Accel();
run99.Accel();
run99.ShowCarState();
run99.Break();
run99.ShowCarState();
Car sped77 = { "sped77",100,0 };
sped77.Accel();
sped77.Break();
sped77.ShowCarState();
return 0;
}