-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassinit.cpp
85 lines (79 loc) · 1.32 KB
/
classinit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <cstring>
using namespace std;
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 fuelGauge;
int curSpeed;
public:
void InitMembers(char * ID, int fuel);
void ShowCarState();
void Accel();
void Break();
};
void Car::InitMembers(char * ID, int fuel)
{
strcpy(gamerID, ID);
fuelGauge=fuel;
curSpeed=0;
};
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(void)
{
Car run99;
run99.InitMembers("run99", 100);
run99.Accel();
run99.Accel();
run99.Accel();
run99.ShowCarState();
run99.Break();
run99.ShowCarState();
Car run100 = run99;
// Car run101 = {"run101", 100};
// run99 = {"run101", 100};
run100.ShowCarState();
run99.Accel();
run100 = run99;
run100.ShowCarState();
return 0;
}