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