title: c++ virtual classes: interesting point
body: <p>Please tell me why the output is as below for the following program. I am not getting the virtual classes in c++. observe the below code:</p>
  
  <pre><code>class B
  {
  public:
      B(char c = 'a') : m_c(c) {}
  
  public:
      char get_c() const { return m_c; }
      void set_c(char c) { m_c = c; }
  
  private:
      char m_c;
  };
  
  class C: public B
  { };
  
  class D: public B
  { };
  
  class E
      : public C
      , public D
  { };
  
  int main()
  {
      E e;
      C &amp;c = e;
      D &amp;d = e;
      std::cout &lt;&lt; c.get_c();
      d.set_c('b');
      std::cout &lt;&lt; c.get_c() &lt;&lt; std::endl;
      return 0;
  }
  </code></pre>
  
  <p>O/P:  aa
  I expect output would be ab. What would be the reason for getting "aa"??</p>
  
  <p>If i have c.set_c('b')  instead of d.set_c('b') then I will get O/P : "ab", Here also, I am not getting why is it as such. Both c, d are referring to one object only. </p>
  
  <pre><code>class C:virtual public B{};
  class D:virtual public B{};
  </code></pre>
  
  <p>If the class C, class D are inherited virtually from B, then 
  O/P would always be "ab" </p>