I just finished this as a project, the code runs fine, any folks have any tips. It's always nice to have a second set of eyes take a look. Instructions: Car Class: Write a class named Car that has the following: year. An int that holds the cars model year. make. A string object that holds the make of car. speed. An int that holds the cars 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 a object's year and make member variables. The constructor should initialize the speed member variable to 0.
Accessors. Appropriate accessor functions should be created to allow values to be retrieved from an object's year, make, and speed member variables.
accelerate. the accelerate function should add 5 to the speed member variable each time it is called.
brake. The brake function should subtract 5 from the speed member variable each time it is called.
Demonstrate the class in a program that creates a Car object, and then calls accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it. The, call the brake function five times. After each call to the brake function, get the current speed of the car and display it.
**Car.h**
#ifndef CARH
#define CARH
#include <string>
#include <iostream>
using namespace std;
class Car
{
public:
Car(int year, string makee);
void brake();
void accelerate();
void setSpeed(int sp);
int getSpeed();
void setMake(string makee);
string getMake();
void setyearModel(int year);
int getyearModel();
private:
int yearModel;
string make;
int speed;
};
#endif
**Car.cpp**
// Class Default Constructor
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
Car::Car(int year, string makee)
{
yearModel = year;
make = makee;
speed = 0;
}
void Car::brake()
{
speed = speed - 5;
}
void Car::accelerate()
{
speed = speed + 5;
}
void Car::setSpeed(int sp)
{
speed = sp;
}
int Car::getSpeed()
{
return speed;
}
void Car::setMake(string makee)
{
make = makee;
}
string Car::getMake()
{
return make;
}
void Car::setyearModel(int year)
{
yearModel = year;
}
int Car::getyearModel()
{
return yearModel;
}
**Jetta_TDI.cpp**
#include "stdafx.h"
#include <iostream>
#include "Car.h"
// Namespaces utilized in this program
using namespace std;
int main(int argc, char *argv[])
{
Car Volkswagen(2013, "Jetta TDI");
cout << "Accelerate" << endl;
for (int i = 0; i < 5; i++)
{
Volkswagen.accelerate();
cout << "Current Speed: " << Volkswagen.getSpeed() << endl;
}
cout << endl;
cout << "Decelerate" << endl;
for (int i = 0; i < 5; i++)
{
Volkswagen.brake();
cout << "Current Speed: " << Volkswagen.getSpeed() << endl;
}
cout << endl;
system("pause");
return 0;
}