Create a class Car that contains following attributes: The name of car, the direction of car(E,W,N,S) and the position of car(from imaginary zero point)
The class has following member functions: A constructor to initialise the attributes, Turn function to change direction of the car to one step right side. Overload the turn function to change the direction to any side directly. It should accept the direction as parameter.
More function to change the position of car away from zero point. It should accept the distance as parameter.
#include <iostream>
#include <string>
using namespace std;
class Car
{
public:
Car()
{};
Car(string n, string d, float p)
{
m_name = n;
m_direct = d;
m_position = p;
}
void turn()
{
if (m_direct == "E")
{
m_direct == "S";
}
else if (m_direct == "S")
{
m_direct == "W";
}
else if (m_direct == "W")
{
m_direct == "N";
}
else if (m_direct == "W")
{
m_direct == "N";
}
}
void turn(string d)
{
m_direct = d;
}
void set_position(float s)
{
m_position = s;
}
string m_name="No_name";
string m_direct = "E";
float m_position{ 0 };
};
int main()
{
// for example test create elements class Car
Car car_ex("first", "S", 258.0);
// turn right
car_ex.turn();
cout << car_ex.m_direct << endl;
// turn with parametr
car_ex.turn("N");
cout << car_ex.m_direct << endl;
// set position
car_ex.set_position(120);
cout << car_ex.m_position << endl;
return 0;
}
Comments
Leave a comment