Tenzin N.

asked • 05/15/20

This question is based on constructors and accessors in c programming!!!!

Car class

Write a class named Car that has the following member variables:

* an int that holds the year the car was made

* a string that holds the make of the car.

* an int that holds the car’s current speed.

* an int that holds the car’s top speed.

In addition, the class should have the following constructor and other member functions

* Constructor -> The constructor should accept the car’s year, make, and top speed as arguments. These values should be assigned to the relevant member variables. The constructor should also assign 0 to the speed member variable.

* Accessor -> Write appropriate accessor functions to get the values stored in an object’s year, make, and speed member variables. These accessors should return the car’s instance variables, rather than directly printing them.

* accelerate -> objects of the Car class should have a member function named accelerate that adds 5 to the speed member variable each time it is called

* brake -> There should also be a brake function that subtracts 5 from the speed member variable each time it is called.

If a call to accelerate would cause the car’s speed to increase above the top speed, set the car’s speed to the top speed. If a call to brake would cause the car’s speed to decrease below 0, set the car’s speed to 0.

Demonstrate the class in a program that creates a Car object with year 1976, make “Trabant”, and top speed 47, then calls the Car’s accelerate function 10 times. Then, call the brake function 10 times. After each call to the brake function, get the car’s current speed and display it.

Put your work in two files: Car.cpp and Car.h. Include the program’s main function in Car.cpp



THIS CODE SHOULD BE IN A FORM OF C++ PROGRAMMING THAT START WITH #include<iostream> AND using namespace std;

Hai D.

tutor
#include <iostream> #include <cstring> #include <cctype> #include <conio.h> using namespace std; class Car { private: int YearModel; int Speed; string Make; public: Car(int, string); Car(); string getMake(); int getModel(); int getSpeed(); void Accelerate(); void Brake(); }; Car::Car() {cout<<"Enter car year: "; cin>>YearModel; cout<<"Enter car make: "; cin>>Make; Speed=0; } Car::Car(int YearofModel, string Makeby) { YearModel = YearofModel; Make = Makeby; Speed = Spd; } //To get who makes the car. string Car::getMake() { return Make; } //To get the year of the car. int Car::getModel() { return YearModel; } //To holds the car actual speed. int Car::getSpeed() { return Speed; } //To increase speed by 5. void Car::Accelerate() { Speed = Speed +5; cout<<"The "<<YearModel<<" "<<Make<<" is accelerating. The speed is now "<<Speed<<" mph."<<endl; } //To drop the speed of the car by 5. void Car::Brake() { Speed = Speed -5; cout<<"The "<<YearModel<<" "<<Make<<" is braking. The speed is now "<<Speed<<" mph."<<endl; } int main() { int Speed = 0; //Start Cars speed at zero. int index; int total=0; Car mine; mine.Accelerate(); mine.Brake(); cout<<"Your Caprice's speed is: "<<Speed<<endl; Car first( 1990, "Chevy", Speed); total+=Speed; //Display the menu and get a valid selection for(index=0;index<6;index++) { first.Accelerate(); // here u have to change Speed to //first.getSpeed() } for(index=0;index<6;index++) { first.Brake(); // here u have to change Speed to //first.getSpeed() } _getch(); return 0; }
Report

05/15/20

Patrick B.

I used Bloodshed Dev C/C++ I have uploaded these source codes files for you in the RESOURCES section under the TOOLKIT menu; the filename is CAR Object C++ and it is under the link to this posting. You will have to break that file into 3 separate files: Car.h, Car.cpp, and main.cpp; then create a new project in your editor, recompile, link, and run
Report

05/16/20

1 Expert Answer

By:

Patrick B. answered • 05/16/20

Tutor
4.7 (31)

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.