title: C++ Constructor Car Class
body: <p><strong>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!</strong></p>
  
  <p><strong>Here is the assignment text:</strong></p>
  
  <p><em>Car Class Instructions:</em> Write a class named 'Car' that has the following member variables: </p>
  
  <p>year. An int that holds the car's model year. </p>
  
  <p>make. A string object that holds the make of the car. </p>
  
  <p>speed. An int object that holds the car's current speed. </p>
  
  <p>In addition, the class should have the following member functions: </p>
  
  <p>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. </p>
  
  <p>Accessors. Appropriate accessor functions should be created to allow values to be retrieved from an object's year, make and speed member variables. </p>
  
  <p>Accelerate. The accelerate function should add 5 to the speed member variable each time it is called. </p>
  
  <p>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.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  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 &gt; 5 )
          speed -=5;
      else speed = 0 ;
  }
  
  
  int main ()
  {
      int year;
      string make;
      cout &lt;&lt; "Please enter the model year of the car.\n";
      cin &gt;&gt; year ;
      cout &lt;&lt; "Please enter the make of the car.\n";
      cin &gt;&gt; make ;
  
      Car myCar(year,make);
      int i = 0;
      for (; i&lt;5; ++i)
      {
          myCar.accelerate();
          cout &lt;&lt; "Accelerating.\n" &lt;&lt; "The current speed of the car is: " &lt;&lt; myCar.getSpeed()&lt;&lt;endl;
      }
  
      {
          int j = 0;
          for (; j&lt;5; ++j)
          {
              myCar.brake();
              cout &lt;&lt; "Decelerating.\n" &lt;&lt; "The current speed of the car is: " &lt;&lt; myCar.getSpeed()&lt;&lt;endl;
          }
  
          return (0);
      }
  }
  </code></pre>