
Tenzin N.
asked 05/15/20This 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;
1 Expert Answer

Patrick B. answered 05/16/20
Math and computer tutor/teacher
//******* Car.h ********************//
#ifndef _CAR
#define _CAR
#define CAR_MAKE_STR_LEN (255)
typedef
class Car
{
private:
int year;
char make[CAR_MAKE_STR_LEN];
int curSpeed;
int maxSpeed;
public:
Car ( int yr, char * strMake, int MaxSpeed);
inline int GetYear() { return(year); }
inline int GetMaxSpeed() { return(maxSpeed); }
inline int GetCurrentSpeed() { return(curSpeed); }
void GetMake( char * carMakeStrBuff);
void accelerate();
void brake();
}*TCar;
#define CAR_SIZE (sizeof(Car))
#define TCAR_SIZE (sizeof(TCar))
#endif
//****** Car.cpp *********************//
#include "car.h"
#endif
Car::Car ( int yr, char * strMake, int MaxSpeed)
{
year = yr;
memset(make,0,CAR_MAKE_STR_LEN);
strncpy(make,strMake,CAR_MAKE_STR_LEN);
maxSpeed = MaxSpeed;
curSpeed=0;
}
void Car::GetMake( char * carMakeStrBuff)
{
memset(carMakeStrBuff,0,CAR_MAKE_STR_LEN);
strcpy(carMakeStrBuff,make);
}
void Car::accelerate()
{
curSpeed += 5;
if (curSpeed>maxSpeed)
{
curSpeed = maxSpeed;
}
}
void Car::brake()
{
curSpeed -= 5;
if (curSpeed<0)
{
curSpeed=0;
}
}
//**** main.cpp *****//
using namespace std;
#include <iostream>
#ifndef _CAR
#include "car.h"
#endif
int main(int argc, char** argv)
{
Car car(1976,(char *)"Trabant",47); // these cars went out of production 30 years ago !!!
char carMakeStr[CAR_MAKE_STR_LEN];
car.GetMake(carMakeStr);
cout << "Now driving car :" << car.GetYear() <<" " << carMakeStr << " with a max speed of " << car.GetMaxSpeed() << endl;
for (int iLoop=0; iLoop<10; iLoop++)
{
car.accelerate();
cout << " current speed is " << car.GetCurrentSpeed() << endl;
}
for (int iLoop=0; iLoop<10; iLoop++)
{
car.brake();
cout << " current speed is " << car.GetCurrentSpeed() << endl;
}
return 0;
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Hai D.
05/15/20