title: c++ inheritance : confused with base member access from derived object
body: <p>Declaration:</p>
  
  <pre><code>class base{
  public:
      base(){a=0;}
      base(int a);
      int getA();
      virtual ~base() ;
  protected:
      int a ;
  };
  //
  class derived : public base {
  public:
      derived(){}
      derived(int a, int b) ;
      int getC() ;
      ~derived();
  private:
      int c ;
  };
  </code></pre>
  
  <p>Definition :</p>
  
  <pre><code>base::base(int a){
      cout &lt;&lt; "   in base constructor a = " &lt;&lt; a &lt;&lt; endl ;
      a = a ;
  }
  base::~base(){
      a = 0 ;
  }
  int base::getA(){
      return a ;
  }
  //
  derived::derived(int a, int b)
  :base(a)
  {
      c = a+b ;
  }
  int derived::getC()
  {
      return c ;
  }
  derived::~derived(){
      c = 0 ;
  }
  </code></pre>
  
  <p>Call:</p>
  
  <pre><code>derived A(4,5) ;
  cout &lt;&lt; "   a of A = " &lt;&lt; A.getA() &lt;&lt; "\n" ;
  cout &lt;&lt; "   c of A = " &lt;&lt; A.getC() &lt;&lt; "\n" ;
  </code></pre>
  
  <p>Result :</p>
  
  <pre><code>in base constructor a = 4
  a of A = 4205648
  c of A = 9
  </code></pre>
  
  <p>Please can someone explain why I am getting this result instead of :</p>
  
  <pre><code>a of A = 4 
  </code></pre>
  
  <p>? Why does the value of base member a change ? Based on what I've learned about inheritance in c++, when we create an object of derived class, this object will inherit all members and member functions of base class, why is it that the member a of object <code>A</code> of class derived loses its value outside the derived class definition ? </p>
  
  <p>Thanks !</p>