Create a class motorcycle that contains the following attributes: The name of motorcycle, the direction of motorcycle (E, W, N, S) and the position of motorcycle (from imaginary zero point) The class has following member functions: A constructor to initialize the attributes, Turn function to change direction of the motorcycle 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 motorcycle away from zero point. It should accept the distance as parameter
#include<iostream>
#include<string>
using namespace std;
class motorcycle{
private:
string name;
string direction;
int pos;
public:
motorcycle(){
}
//Constructor
motorcycle(string n, string d, int p): name(n), direction(d), pos(p){};
void turn(){
if(direction=="E"){
direction = "S";
}
else if(direction=="S"){
direction = "W";
}
else if(direction=="W"){
direction = "N";
}
else if(direction=="N"){
direction = "E";
}
}
void turn(string direct){
direction = direct;
}
void changePosition(int distance){
pos = distance;
}
string motorcycleDetails(){
string finalString = " Motorcycle name: "+name+"\nMotorcycle direction: "+direction;
string last= "\nDistance of the motorcycle from the imaginary point: "+to_string(pos);
return finalString + last;
}
};
//Testing Code
int main(){
motorcycle m("Honda","N",2);
cout<<"Motorcycle details are\n"<<m.motorcycleDetails();
}
Comments
Leave a comment