Answer to Question #187793 in Programming & Computer Science for Zara

Question #187793

: 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:


1
Expert's answer
2021-05-03T12:20:20-0400
#include <iostream>
#include <string>

using namespace std;
class Bus{
private:
	string name;
	string direction;
	int position;
public:
	Bus(string n,string d,int p){
		this->name=n;
		this->direction=d;
		this->position=p;
	}
	void changeDirection(){
		if(direction.compare("East")==0){
			direction="South";
		}else if(direction.compare("South")==0){
			direction="West";
		}else if(direction.compare("West")==0){
			direction="North";
		}else if(direction.compare("North")==0){
			direction="East";
		}
		cout<<"A new direction is: "<<direction<<"\n";
	}
	void changeDirection(string newDirection){
		direction=newDirection;
		cout<<"A new direction is: "<<direction<<"\n";
	}
	void changePosition(int position){
		position=position;
		cout<<"A new position is: "<<position<<"\n";
	}
};
int main(){
	Bus myBus("My Bus","North",15);
	myBus.changeDirection();
	myBus.changeDirection("South");
	myBus.changePosition(25);

	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