c++ - I am having trouble understanding why does the output change after removing a variable from a subclass -


hello in code below output "a", don't understand why output (the 'value' variable) changes "b" after removing value variable subclass b

#include <iostream> #include <string>  using namespace std;  class { public:      string value;     a(){         value = "a";     }      void display(){         cout<<value<<endl;     }  };  class b : public a{ public:      string value;     b(){         value = "b";     }   };  int main(){     *c = new b();     c->display();  } 

when both classes have variable named value b constructor refers b::value.

after removed b, started referring a::value, , assigning it.

make variables a::value private, , you'll see compiler complain try access inaccessible member variable.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -