Answer to Question #227752 in C++ for mickey

Question #227752

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


1
Expert's answer
2021-08-19T14:31:22-0400
#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();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS