title: Car Class / Constructor C++
body: <p><strong>I have been beating my head against the wall trying to figure out the "Constructor" part of this assignment. This includes reading the textbook and looking around online. Can someone please point me in the right direction on this? The below code works perfectly, just need the addition of a Constructor.</strong></p>
  
  <p><strong>Here are the instructions for this assignment:</strong>
  Car Class Instructions: </p>
  
  <p>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. </p>
  
  <p>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 = 0;
  
  public:
  void setYear(int);
  void setMake(string);
  void setSpeed(int);
  int getYear();
  string getMake();
  int getSpeed();
  void accelerate();
  void brake();
  
  
  };
  void Car::setYear(int y)
  {year = y;}
  int Car::getYear(){
  return year;}
  
  void Car::setMake(string m)
  {make = m;}
  string Car::getMake(){
  return make;}
  
  void Car::setSpeed(int spd)
  {speed = spd;}
  int Car::getSpeed(){
  return speed;}
  
  void Car::accelerate()
  {speed +=5;}
  
  void Car::brake()
  {
  if( speed &gt; 5 )
      speed -=5;
  else speed = 0 ;
  }
  
  
  int main()
  {
  Car myCar;
  int bYear = 0;
  string bMake;
  cout &lt;&lt; "Please enter the year of the vehicle.\n";
  cin &gt;&gt; bYear;
  cout &lt;&lt; "Please enter the make of the vehicle.\n";
  cin &gt;&gt; bMake;
  
  myCar.setYear(bYear);
  cout &lt;&lt; "You entered the year of the car as " &lt;&lt; myCar.getYear() &lt;&lt; endl;
  myCar.setMake(bMake);
  cout &lt;&lt; "You entered the make of the car as " &lt;&lt; myCar.getMake() &lt;&lt; endl;
  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;
  
      }
  
  
  
  }
  }
  </code></pre>