-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcarClass.cpp
More file actions
146 lines (126 loc) · 3.46 KB
/
carClass.cpp
File metadata and controls
146 lines (126 loc) · 3.46 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
using namespace std;
enum direct{NORTH, EAST, SOUTH, WEST}; //enum for direction
class Car //class for the car
{
private: //attributes of the car and direction its going
string model;
int year;
int mileage;
direct direction;
int x;
int y;
public: //functions
Car(string md, int yr); //what we should know
string getModel() const;
int getYear() const;
void goForward(int block);
int getMileage() const;
int getx() const;
int gety() const;
void turnRight();
string getDirection() const;
void turnLeft();
};
Car::Car(string md, int yr) //set up to print
{
model = md;
year = yr;
mileage = 0;
direction = NORTH;
x = 0;
y = 0;
}
string Car::getModel() const //prints model of car
{
return model;
}
int Car::getYear() const //function to print the year
{
return year;
}
void Car::goForward(int block) //function to move the car in the block and get mileage
{
if(direction == NORTH) //adds y heading north
y += block;
else if(direction == EAST) //adds x heading east
x += block;
else if(direction == SOUTH) //subtracts y heading south
y -= block;
else if(direction == WEST) //subtracts x heading west
x -= block;
mileage += block; //calculates mileage
}
int Car::getMileage() const //function to print the mileage
{
return mileage;
}
int Car::getx() const//gets x value of where block is
{
return x;
}
int Car::gety() const //gets y value of where car is
{
return y;
}
void Car::turnRight() //to change the direction of the car
{
if(direction == WEST) //if the direction is at west, the next right will be north (because 4 + 1 is not 0)
direction = NORTH;
else //NORTH, EAST, SOUTH
direction = (direct)(direction + 1); //for all the other ones, it'll be the whatever the next direction is
}
void Car::turnLeft() //to change the direction of the car
{
if(direction == NORTH) //if the direction is at east, the next left will be north (because 0 - 1 is not 4
direction = WEST;
else //EAST, WEST, SOUTH
direction = (direct)(direction - 1); //for all the other ones, it'll be the whatever the next direction is
}
string Car::getDirection() const //to get the direction it is heading to
{
switch(direction) //switch so that it prints out the string and not the int
{
case NORTH: return "North";
case EAST: return "East";
case SOUTH: return "South";
case WEST: return "West";
}
}
void showInfo(const Car c); //prototype for showInfo
int main()
{
Car c1("Camry", 2019); //creats c1 car
Car c2("Neon", 2020); //creats c2 car
//outputs:
cout << c1.getModel() << endl;
cout << c1.getDirection() << endl;
c1.goForward(3);
c1.turnRight();
c1.goForward(5);
c1.turnRight();
c1.goForward(7);
c1.turnRight();
c1.goForward(6);
c1.turnLeft();
c1.goForward(1);
cout << "Ends up at (" << c1.getx() << "," << c1.gety() << ")" << endl;
cout << "Facing " << c1.getDirection() << endl;
cout << c1.getMileage() << endl;
showInfo(c1); //shows the entire info of c1
c2.goForward(5);
c2.turnLeft();
c2.goForward(1);
c2.turnLeft();
c2.goForward(2);
c2.turnLeft();
c2.goForward(4);
c2.turnLeft();
c2.goForward(10);
showInfo(c2); //shows entire info of c2
return 0;
}
void showInfo(const Car c) //to show info of whatever car is created
{
cout << c.getModel() << " (" << c.getYear() << ") is located at (" << c.getx() << "," << c.gety() << ") facing " << c.getDirection() << ". It has " << c.getMileage() << " miles on it. " << endl ;
}