Don Won
2010-12-07 19:35:07 UTC
#include
#include
#include
using namespace std;
class Car
{
private:
int yearModel;
string make;
int speed;
public:
Car(int , string , int); // constructor
void setYearModel(int);
void setMake(string);
void setSpeed(int);
int getYearModel() const;
string getMake() const;
int getSpeed() const;
int accelerate();
int brake();
};
Car::Car(int yr, string mk, int sp )
{
yearModel = yr;
make = mk;
speed = sp;
}
void Car::setYearModel(int m)
{
yearModel = m;
}
void Car::setMake(string mk)
{
make = mk;
}
void Car::setSpeed(int sp)
{
speed = sp;
}
int Car::getYearModel() const
{
return yearModel;
}
string Car::getMake() const
{
return make;
}
int Car::getSpeed() const
{
return speed;
}
int Car::accelerate()
{
speed += 5;
return speed;
}
int Car::brake()
{
speed -= 5;
return speed;
}
int main()
{
string carMake;
int carSpeed = 0;
int caryearModel;
// Get the car make
cout << "Enter the make of the car: ";
cin >> carMake;
// Get the year of the car
cout << endl << "Enter the year of the car: ";
cin >> caryearModel;
cout << endl;
Car frame( caryearModel,carMake, carSpeed);
// Increase the car speed
cout << "INCREASING:" << endl;
for(int i = 0; i < 5; i++)
{
cout << "The speed is now " << frame.accelerate() << endl;
}
// Decrease the car speed
cout << endl << endl << "DECREASING:" << endl;
for(int i = 0; i < 5; i++)
{
cout << "The speed is now " << frame.brake() << endl;
}
// display the car speed, make and year model
cout << setprecision(2) << fixed;
cout << endl << "The make of the car is " << frame.getMake();
cout << endl << "The year of the model is " << frame.getYearModel();
cout << endl << "The speed of the car is " << frame.getSpeed() << endl << endl;
system("pause");
return 0;
}