Answer to Question #187804 in C++ for Salman khan

Question #187804

Write a class named as Bus that contains the attributes which are mentioned below:

 The name of Bus.

 The direction of Bus (North (N), South(S), East(E), West (W))

 The position of Bus (from imaginary zero point)

The class has the following member functions:

 A constructor to initialize the attributes

 Turn function to change the direction of bus to one step right side (e.g if the direction is 

to East, it should be changed to South and so on)

 Overload the Turn function to change the direction to any side directly. It should take the 

direction as an argument.

 Move function to change the position of the Bus away from imaginary zero point. It 

should accept the distance as an argument


1
Expert's answer
2021-05-01T14:10:26-0400
#include <iostream>
#include <string>

using namespace std;


class Bus{
private:
	string name;
	string direction;// direction of Bus (North (N), South(S), East(E), West (W))
	//the position of Bus (from imaginary zero point)
	int position;
public:
	//constructor to initialize the attributes
	Bus(string name,string direction,int position){
		this->name=name;
		this->direction=direction;
		this->position=position;
	}
	//Turn function to change the direction of bus to one step right side (e.g if the direction is 
	//to East, it should be changed to South and so on)
	void changeDirection(){
		if(this->direction.compare("East")==0){
			this->direction="South";
		}else if(this->direction.compare("South")==0){
			this->direction="West";
		}else if(this->direction.compare("West")==0){
			this->direction="North";
		}else if(this->direction.compare("North")==0){
			this->direction="East";
		}
		cout<<"Current direction: "<<this->direction<<"\n";
	}
	//Overload the Turn function to change the direction to any side directly. It should take the direction as an argument.
	void changeDirection(string direction){
		this->direction=direction;
		cout<<"Current direction: "<<this->direction<<"\n";
	}
	//Move function to change the position of the Bus away from imaginary zero point. It should accept the distance as an argument
	void changePosition(int position){
		this->position=position;
		cout<<"Current position: "<<this->position<<"\n";
	}
};


int main(){
	Bus bus("Bus 45","East",5);
	bus.changeDirection();
	bus.changeDirection("North");
	bus.changePosition(5);




	system("pause");
	return 0;
}

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