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
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 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 car class with three attributes: year, mpg, speed and list of owners. The class...
Create a car class with three attributes: year, mpg, speed and list of owners. The class also should have 2 methods called accelerate and brake. Whenever the car accelerates, its speed is increased by 30 and whenever the car brakes, its speed is reduced by 60. add a constructor to the class, which takes year, mpg, and speed as input implement a magic method that prints the car information including year, speed and mpg. That is, for a car object,...
Java Write a class called Car that contains instance data that represents the make, model, and...
Java Write a class called Car that contains instance data that represents the make, model, and year of the car. Define the Car constructor to initialize these values Include getter and setter methods for all instance data and a toString method that returns a one-line description of the car. Add a method called isAntique that returns a boolean indicating if the car is an antique (if it is more than 45 years old). Create a driver class called CarTest, whose...
Write a class encapsulating the concept of a student, assuming a student has the following attributes:...
Write a class encapsulating the concept of a student, assuming a student has the following attributes: a name, a Social Security number, and a GPA (for instance, 3.5). Include a constructor, the accessors and mutators, and methods toString and equals. Write a client class (test driver with main method) to test all the methods in your class (create 2 student objects, print the name, social security number and GPA of the 2 students, check if the two student’s GPA is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT