C++
- @Linda_pp
- 清楚なC++メイドBOT
- 長谷川一輝
- @jj1bdx
- 安藤敏彦
- Siv3D
- @hnokx
- @ishidakei
- TAKEI Yuya
- I (@wx257osn2)
- Tommy6
- @nekketsuuu
- LouiS0616
- @volanja
- 大鎌広
- むてら
- ガチKGB
- 三重野賢人
x
76
1
2
3
using namespace std;
4
5
class Car
6
{
7
private:
8
int year;
9
string make;
10
int speed;
11
12
public:
13
Car(int, string, int);
14
int getSpeed();
15
int getModel();
16
void accelerate();
17
void brake();
18
19
20
};
21
22
int Car::getSpeed()
23
{
24
return speed;
25
}
26
27
Car::Car(int year, string make, int speed = 0 )
28
{
29
}
30
31
void Car::accelerate()
32
{
33
speed +=5;
34
}
35
36
void Car::brake()
37
{
38
if( speed > 5 )
39
speed -=5;
40
else speed = 0 ;
41
}
42
43
int main ()
44
{
45
int year;
46
47
string model;
48
int control;
49
cout<<"Please enter the year of the car. "<<endl;
50
cin>>year ;
51
cout<<"Please enter the make/model of the car. "<<endl;
52
cin>>model;
53
cout<<"Enter 1 to accelerate and enter 2 to break."<<endl;
54
cin>>control;
55
56
Car carOne(year,model);
57
58
if (control == 1) {
59
60
carOne.accelerate();
61
62
cout<<"Accelerating."<<"The current speed of the car is: " <<carOne.getSpeed() <<endl;
63
}
64
65
else if (control == 2) {
66
67
carOne.brake();
68
69
cout<<"Decelerating. "<<"The speed of the car is: "<< carOne.getSpeed()<<endl;
70
}
71
72
else {
73
cout<<"Invalid input. Please try again. "<<endl;
74
}
75
return (0);
76
}
$ g++ prog.cc -Wall -Wextra -I/opt/wandbox/boost-1.73.0/gcc-10.1.0/include -std=gnu++2a
Stdin
1970
ranborugiini
1
Start
prog.cc: In constructor 'Car::Car(int, std::string, int)': prog.cc:27:14: warning: unused parameter 'year' [-Wunused-parameter] 27 | Car::Car(int year, string make, int speed = 0 ) | ~~~~^~~~ prog.cc:27:27: warning: unused parameter 'make' [-Wunused-parameter] 27 | Car::Car(int year, string make, int speed = 0 ) | ~~~~~~~^~~~ prog.cc:27:37: warning: unused parameter 'speed' [-Wunused-parameter] 27 | Car::Car(int year, string make, int speed = 0 ) | ~~~~^~~~~~~~~
Please enter the year of the car. Please enter the make/model of the car. Enter 1 to accelerate and enter 2 to break. Accelerating.The current speed of the car is: -1727144363
0
Finish