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
Post a Comment