Question

In: Computer Science

Write a class Car that contains the following attributes: The name of car The direction of...

Write a class Car that contains the following attributes:

  • The name of car
  • The direction of car (E, W, N, S)
  • The position of car (from imaginary zero point) The class has the following member functions:
  • A constructor to initialize the attributes.
  • Turn function to change the direction of car to one step right side (e.g. if the direction is to E,it should be changed to S and so on.)
  • Overload the Turn function to change the direction to any side directly. It should accept the direction as parameter.

Solutions

Expert Solution

Below is your program

#include <string.h>

#include <iostream>

using namespace std;

class car

{

int pos;

char car_name[20];

char dir;

public:

void turn();

void move(int);

void turn(char);

car(char name[], char d, int p)

{

strcpy(car_name,name);

dir=d;

pos=p;

}

void display() {

cout<<"\nCar Name: "<<car_name<<", Position: "<<pos<<", Direction: "<<dir<<endl;

}

};

void car :: turn()

{

switch(dir)

{

case 'N' : dir='E';

break;

case 'E' : dir='S';

break;

case 'S' : dir='W';

break;

case 'W' : dir='N';

break;

default : cout<<"THE DIRECTION IS INCORRECT !!!";

}

}

void car :: turn(char d1)

{

dir=d1;

}

void car :: move(int p1)

{

pos+=p1;

}

int main()

{

class car c("NANO",'N',100);

char ch;

char dir;

int p1,ch1;

c.display();

bool done = false;

while(!done)

{

cout<<"\n1.change in direction sequntially";

cout<<"\n2.change in direction randomly";

cout<<"\n3.change in position of the car";

cout<<"\n4.Exit";

cout<<"\nEnter your choice\n";

cin>>ch1;

switch(ch1)

{

case 1:

c.turn();

break;

case 2:

cout<<"input the required direction\n ";

cin>>dir;

c.turn(dir);

break;

case 3:

cout<<"\nEnter the require position\n";

cin>>p1;

c.move(p1);

break;

case 4:

done = true;

break;

default:

cout<<"\nWrong choice";

}

cout<<"Car Details: "<<endl;

c.display();

}

return 0;

}

Output


Car Name: NANO, Position: 100, Direction: N

1.change in direction sequntially
2.change in direction randomly
3.change in position of the car
4.Exit
Enter your choice
1
Car Details:

Car Name: NANO, Position: 100, Direction: E

1.change in direction sequntially
2.change in direction randomly
3.change in position of the car
4.Exit
Enter your choice
1
Car Details:

Car Name: NANO, Position: 100, Direction: S

1.change in direction sequntially
2.change in direction randomly
3.change in position of the car
4.Exit
Enter your choice
1
Car Details:

Car Name: NANO, Position: 100, Direction: W

1.change in direction sequntially
2.change in direction randomly
3.change in position of the car
4.Exit
Enter your choice
2
input the required direction
N
Car Details:

Car Name: NANO, Position: 100, Direction: N

1.change in direction sequntially
2.change in direction randomly
3.change in position of the car
4.Exit
Enter your choice
1
Car Details:

Car Name: NANO, Position: 100, Direction: E

1.change in direction sequntially
2.change in direction randomly
3.change in position of the car
4.Exit
Enter your choice
3

Enter the require position
50
Car Details:

Car Name: NANO, Position: 150, Direction: E

1.change in direction sequntially
2.change in direction randomly
3.change in position of the car
4.Exit
Enter your choice
1
Car Details:

Car Name: NANO, Position: 150, Direction: S

1.change in direction sequntially
2.change in direction randomly
3.change in position of the car
4.Exit
Enter your choice
4

--------------------------------
Process exited after 26.09 seconds with return value 0
Press any key to continue . . .


Related Solutions

Write a class named Person with data attributes for a person’s first name, last name, and...
Write a class named Person with data attributes for a person’s first name, last name, and telephone number. Next, write a class named Customer that is a subclass of the Person class. The Customer class should have a data attribute for a customer number and a Boolean data attribute indicating whether the customer wishes to be on a calling list. Demonstrate an instance of the Customer class in a simple program. Using python
IN JAVA!   Write a class Store which includes the attributes: store name, city. Write another class...
IN JAVA!   Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method...
Write a Class called Module with the following attributes: module code, module name, list of lecturers...
Write a Class called Module with the following attributes: module code, module name, list of lecturers for the module (some modules may have more than one lecturer – we only want to store their names), number of lecture hours, and module description. Create a parameterised (with parameters for all of the class attributes) and a non-parameterised constructor, and have the accessor and mutator methods for each attribute including a toString method. Write a class Student with the following attributes: student...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score (int). A constructor expects a name as a parameter. A method getLevel to get the level(char) of the student. score level table: A: score >= 90 B: score >= 80 and < 90 C: score >= 60 and < 80 D: score < 60 Example:          Student student = new Student("Zack"); student.score = 10; student.getLevel(); // should be 'D'. student.score = 60; student.getLevel(); //...
Write a Python class to represent a Salik account. The account has three attributes, a name,...
Write a Python class to represent a Salik account. The account has three attributes, a name, id, and the balance. The balance is a private attribute. The class has extra functions to add to the balance and reduce the balance. Both, these functions should return the current balance and if the balance is below AED 50.0 print the message “Note: Balance Below 50”. Your class must work for the code given below. #Test myCar = SalikAccount() myCar.setName("John") myCar.setID("190300300333") myCar.setBal(20.0) yourCar...
Write a class Store which includes the attributes: store name and sales tax rate. Write another...
Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Book Store, which inherits from Store. A Book Store has the following additional attributes: how many books are sold every year and the average price per book. Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Book Store; In the Book Store class, also code a method returning the average taxes per year....
Car Class Write a class named Car that has the following member variables: • year. An...
Car Class Write a class named Car that has the following member variables: • year. An int that holds the car’s model year. • make. A string object that holds the make of the car. • speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. • Constructor. The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables....
Using JAVA Create a class Client. Your Client class should include the following attributes: Company Name...
Using JAVA Create a class Client. Your Client class should include the following attributes: Company Name (string) Company id (string) Billing address (string) Billing city (string) Billing state (string) Write a constructor to initialize the above Employee attributes. Create another class HourlyClient that inherits from the Client class.   HourClient must use the inherited parent class variables and add in hourlyRate and hoursBilled. Your Hourly Client class should contain a constructor that calls the constructor from the Client class to initialize...
Create a class Employee. Your Employee class should include the following attributes: First name (string) Last...
Create a class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create another class HourlyEmployee that inherits from the Employee class.   HourEmployee must use the inherited parent class variables and add in HourlyRate and HoursWorked. Your HourEmployee class should contain a constructor that calls the constructor from the...
Create a class Client. Your Client class should include the following attributes: Company Name (string) Company...
Create a class Client. Your Client class should include the following attributes: Company Name (string) Company id (string) Billing address (string) Billing city (string) Billing state (string) Write a constructor to initialize the above Employee attributes. Create another class HourlyClient that inherits from the Client class.   HourClient must use the inherited parent class variables and add in hourlyRate and hoursBilled. Your Hourly Client class should contain a constructor that calls the constructor from the Client class to initialize the common...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT