Iain Kilpatrick Iain Kilpatrick - 24 days ago 11
C++ Question

C++ Constructor Car Class

Hi all, I am having some trouble with the below assignment. When I run the code, everything appears to work, but the numbers that I get in the print out are not accurate. Not sure if it is the constructor I have that is set up wrong or what. I've just been banging my head against the wall trying to figure it out. Any help/advice would be much appreciated!

Here is the assignment text:

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

year. An int that holds the car's model year.

make. A string object that holds the make of the car.

speed. An int object that holds the car's current speed.

In addition, the class should have the following member functions:

Constructor. The constructor should accept the car's year and make member variables. The constructor should initialize these values to the 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 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 5 times. After each call to the brake function, get the current speed of the car and display it.

#include <iostream>
#include <string>

using namespace std;
class Car
{
private:
int year;
string make;
int speed;

public:
Car(int, string, int);
int getSpeed();
int getModel();
void accelerate();
void brake();


};

int Car::getSpeed()
{
return speed;
}

Car::Car(int year, string make, int speed = 0 )
{
}
void Car::accelerate()
{
speed +=5;
}
void Car::brake()
{
if( speed > 5 )
speed -=5;
else speed = 0 ;
}


int main ()
{
int year;
string make;
cout << "Please enter the model year of the car.\n";
cin >> year ;
cout << "Please enter the make of the car.\n";
cin >> make ;

Car myCar(year,make);
int i = 0;
for (; i<5; ++i)
{
myCar.accelerate();
cout << "Accelerating.\n" << "The current speed of the car is: " << myCar.getSpeed()<<endl;
}
{
int j = 0;
for (; j<5; ++j)
{
myCar.brake();
cout << "Decelerating.\n" << "The current speed of the car is: " << myCar.getSpeed()<<endl;
}

return (0);
}
}

Answer

The constructor

Car::Car(int year, string make, int speed = 0 )
{
}

is not doing what you describe. The three parameters (year, make, and speed) have the same names as Cars members, but are separate variables.

The result is that Cars members are default constructed, and the values passed by main() are effectively ignored.

To make things clearer, give the arguments different names, and initialise the members in an initialiser list.

Car::Car(int y, string m, int s) : year(y), make(m), speed(s)
{
}

It is permissible for the arguments to have the same names as class members, but this tends to be harder to read i.e. it is easier to misunderstand what the code is doing. In your case, you believe that giving the parameters the same name as class members serves to initialise the class members, and that is flat-out wrong. Either way, the constructor needs to explicitly initialise the members using the parameters, otherwise the class members receive default values (not the ones passed).

Also, if you want an argument with a default value, specify it in the definition of the class, not in the definition of the constructor. For example;

class Car
{
      public:

          Car(int, std::string, int = 0);
};

Car::Car(int y, string m, int s) : year(y), make(m), speed(s)
{
}

That makes things easier, particularly if the class definition is in a header file.