title: same data members in both base and derived class
body: <p>I am new to C++ programming, i am reading inheritance concept, i have a doubt about inheritance concept which is: what will happen if the base and derived class have same data members. And also please go through my code as below:</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  using namespace std;
  
  class ClassA
  {
     protected :
         int width, height;
     public :
         void set_values(int x, int y)
         {
             width = x;
             height = y;
         }
  };
  class ClassB : public ClassA
  {
      int width, height;
      public :
          int area()
          {
              return (width * height);
          }
  };
  
  int main()
  {
      ClassB Obj;
      Obj.set_values(10, 20);
      cout &lt;&lt; Obj.area() &lt;&lt; endl;
      return 0;
   }
  </code></pre>
  
  <p>In the above i am declaring data members with the same name as Base class data members, and i called the <code>set_values()</code> function with the derived Class Object to initialize the data members <code>width</code> and <code>height</code>.  </p>
  
  <p>When i call the <code>area()</code> function, why did it return some garbage value instead of returning the proper value. And it is happening only when i declare data members with same name as base class data members in derived class. If i remove the data members declared in derived class it is working fine. So what is the problem with declaration in derived class? please help me. </p>