I am totally lost on this now, I have been trying to do it for 3 days now but am totally failing at it. There are a lot of details, so on the off chance someone wants to help, it is quite a read....
Write a class named Car that has the following member variables:
year
An int that holds the car’s model year. The year must be between 1900-2030 (inclusive).
make
A string that holds the make of the car.
speed
An int that holds the car’s current speed. The speed must be 0 or greater.
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. The constructor should initialize the speed member variable to 0.
Notice that this constructor will have 2 parameters: one for the year and one for the make of the Car.
There is NO default constructor. That means you will not be able to define a Car variable like this:
Car myCar;
The above statement will try to call the default constructor which does not exist. When you define a Car object, you will need to send the two arguments the constructor is expecting:
Car myCar(2002, “Jetta”);
In the constructor you need to assign a value to each of the member variables:
Since year has specific values that are allowed, call the setYear function with the value sent.
Set make to the value passed.
Set speed to zero. You can do this with a simple assignment statement or you can call the setSpeed function.
Accessors
Appropriate accessor functions should be created to allow values to be retrieved from an object’s year, make, and speed member variables.
There will be three get functions, one for each member variable. Each of these functions return one thing and have NO parameters.
There will be three set functions, one for each member variable. Each of these functions receive one thing and return nothing. The speed cannot be less than zero. Have your set function protect the speed so it is never negative. If a negative value is sent, simply set the speed to zero. The year must be between 1900 and 2030 (inclusive). If a year outside the range is sent, set the year to 2012. No validation of the make is required.
accelerate
The accelerate function should add 5 to the speed member variable each time it is called. This member function returns nothing and has no parameter. It simply does its job: each time it is called, it adds 5 to speed.
brake
The brake function should subtract 5 from the speed member variable each time it is called. If the speed is already zero, no change is to be made to speed. This member function returns nothing and has no parameter. It simply does its job: each time it is called, it subtracts 5 from speed unless speed is already at zero. Then it makes no change.
Demonstrate the class in a program that creates a Car object, and then calls the accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it. Then, call the brake function seven times. After each call to the brake function, get the current speed of the car and display it. Each time you display the speed include the year and make of the vehicle in the output label.
Now, set the year, make, and speed to 2009, Jeep, and 75 respectively. Once again call the accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it. Then, call the brake function seven times. After each call to the brake function, get the current speed of the car and display it. Each time you display the speed include the year and make of the vehicle in the output label.
Finally, set the year, make, and speed to 1827, Jetta, and -30 respectively. Using the get functions, print out the year, make, and speed.
Here is what I have currently. Its not complete as I have had troubles with just about everything and have rinsed and repeated multiple times.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#include <iostream>
#include <string>
using namespace std;
string getMake();
int getYear();
void setSpeed(int);
void setYear(int);
void setMake(string);
class Car
{
private:
int year,speed;
string make;
public:
Car(int newYear, string newMake)
{
year=newYear;
void setYear();
make=newMake;
speed=0;
};
int getSpeed() //get speed function
{return speed;}
void Car::setYear(int newYear) //function to set year to 2012 if it receives a value inbewteen 1900 and 2030
{
if(newYear<1900&&newYear>2030)
year=2012;
else
year = newYear;
}
void setSpeed(int speed) //function to set the speed to 0 if it recives a negitive value
{
if(speed<0)
speed=0;
else
speed=speed;
}
void accelerate(){ speed+=5;}
void brake() //function to subtract five from speed every time it is called
{
if(speed>=5)
speed-=5;
else
speed=0;
}
int getYear()
{
return year;
}
string getMake()
{
return make;
}
int main()
{
Car objectCar(2010,"Mazda");
cout<<"Accelerating...\n\n";
for (int gainingSpeed=1; gainingSpeed<=5; gainingSpeed++)
{ objectCar.accelerate();
cout<<"The current speed of the "<<objectCar.getYear<<" "<<objectCar.getMake<<" "<<"is :"<<objectCar.getSpeed()<<endl;
}
cout<<"Braking.....\n\n";
for (int braking=1; braking<=7; braking++)
{
objectCar.brake();
cout<<"The current speed of the "<<objectCar.getYear()<<" "<<objectCar.getMake()<<" "<<"is :"<<objectCar.getSpeed()<<endl;
}
Car objectCar(2009,"Jeep");
cout<<"Accelerating...\n\n";
for (int gainingSpeed=1; gainingSpeed<=5; gainingSpeed++)
{ objectCar.accelerate();
cout<<"The current speed of the "<<objectCar.getYear()<<" "<<objectCar.getMake<<" "<<"is :"<<objectCar.getSpeed()<<endl;
}
cout<<"Braking.....\n\n";
for (int braking=1; braking<=7; braking++)
{
objectCar.brake();
cout<<"The current speed of the "<<objectCar.getYear()<<" "<<objectCar.getMake()<<" "<<"is :"<<objectCar.getSpeed()<<endl;
}
return 0;
}}
|