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
#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;
}
Comments
Leave a comment