Answer to Question #187799 in C++ for umer

Question #187799

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 (eg 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:40-0400
#include <iostream>

using namespace std;


class Bus{
private:
	string name;
	char direction;
	int position;
public:
	Bus(string n,char d,int p){
		name=n;
		direction=d;
		position=p;
	}
	void changeDirection(){
		if(direction=='E'){
			direction='S';
		}else if(direction=='S'){
			direction='W';
		}else if(direction=='W'){
			direction='N';
		}else if(direction=='N'){
			direction='E';
		}
	}
	void changeDirection(char d){
		direction=d;
	}
	void changePosition(int p){
		position=p;
	}
};


int main(){
	Bus currentBus("Bus",'S',10);
	currentBus.changeDirection();
	currentBus.changeDirection('W');
	currentBus.changePosition(5);


	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