title: C++ Member Functions and Constructor problems
body: <pre><code>#include&lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  
  class Car{
  private: 
  
  public: Car(int, string, int);
      Car(){}  //default constructor 
      void setbrake();
      void setacc();
      int year,speed;
       string make;
  
  };
  
  
  Car::Car(int year, string make, int speed = 0) //constructor with parameters
  { 
  cout &lt;&lt; "Enter your car's year." &lt;&lt; endl;
  cin &gt;&gt; year;
  cout &lt;&lt; "Enter your car's make. " &lt;&lt; endl;
  cin &gt;&gt; make;
  cin.ignore();
  speed = 0; }
  
  void Car::setbrake(){  //first function
      speed = speed - 5;
      cout &lt;&lt; "Your " &lt;&lt; make &lt;&lt; " is traveling at " &lt;&lt; speed;
      cout &lt;&lt; " MPH.";}
  
  void Car::setacc(){   //second function
      speed = speed + 5;
      cout &lt;&lt; "Your " &lt;&lt; make &lt;&lt; " is traveling at " &lt;&lt; speed;
      cout &lt;&lt; " MPH.";}
  
  
  
  
  int main()
  {
  Car car1(0,"s");
  cout &lt;&lt; car1.make;
  cout &lt;&lt; car1.speed;
  cout &lt;&lt; car1.year;
  car1.setacc();
  
  
  
  system("pause");
  return 0;
  }
  </code></pre>
  
  <p>I'm having a problem getting my variables to display in my two member functions, and it doesn't display anything in main. 
  The speed variable always comes out to 49898575 &lt;-- some randomly high number like that, and I assume I initialized it to 0. 
  I've been stuck on this all week.
  How do I connect my class private variables and my member functions. 
  I thought my constructor with parameters would take care of that problem, but obviously not.
  Can someone please take the time to critique this code for me. I'm going to pull my hair out. </p>
  
  <p>Here's the problem that was assigned for class, and this is what I'm aiming to do, but I'm not catching on to classes and member functions, and how they work together. </p>
  
  <p>Write a class named Car that has the following member variables:
  • year. An int that holds the car’s model year.</p>
  
  <p>• make. A string that holds the make of the car.</p>
  
  <p>• speed. An int 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 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.</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. </p>
  
  <p>Then, call the brake function five times. </p>
  
  <p>After each call to the brake
  function, get the current speed of the car and display it.</p>