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(); //...
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....
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,...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE-...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE- class-level attribute initialized to 1000 -acctID: int -bank: String -acctType: String (ex: checking, savings) -balance: Float METHODS: <<Constructor>>Account(id:int, bank: String, type:String, bal:float) +getAcctID(void): int                        NOTE: retrieving a CLASS-LEVEL attribute! -setAcctID(newID: int): void           NOTE: PRIVATE method +getBank(void): String +setBank(newBank: String): void +getBalance(void): float +setBalance(newBal: float): void +str(void): String NOTE: Description: prints the information for the account one item per line. For example: Account #:        ...
Write a class called Account that contains: • Three privateinstance variables: name (String), account (String),...
Write a class called Account that contains: • Three private instance variables: name (String), account (String), account balance (double). • One constructor, which constructs all instances with the values given. • Getters and setters for all the instance variables. • Debit function that takes an amount from the user and subtract the balance (make sure the balance will not drop below zero after the operation, if this was the case, the function should return false else it should return true)...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # #...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # # - meat # - to_go # - rice # - beans # - extra_meat (default: False) # - guacamole (default: False) # - cheese (default: False) # - pico (default: False) # - corn (default: False) # #The constructor should let any of these attributes be #changed when the object is instantiated. The attributes #with a default value should be optional. Both positional #and...
Enlistee and Private Classes Write an Enlistee class that keeps data attributes for the following pieces...
Enlistee and Private Classes Write an Enlistee class that keeps data attributes for the following pieces of information: Enlistee name Enlistee number Next, write a class named Private that is a subclass of the Enlistee class. The Private class should keep data attributes for the following information: Platoon number (an integer, such as 1, 2, or 3) Years of service (also an integer) Write the appropriate accessor and mutator methods for each class. Once you have written the classes, write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT