My question is related to the state of memory for an object of a derived C++ class. In this example, the derived class inherits a public member function which manipulates a private member.
class Base {
int a;
public:
int b;
void write_a(int a_in);
int read_a();
};
void Base :: write_a(int a_in){
a = a_in;
}
int Base :: read_a(){
return a;
}
class Derived : public Base {
int c;
public:
void write_c(int c_in);
int read_c();
};
void Derived :: write_c(int c_in){
c = c_in;
}
int Derived :: read_c(){
return c;
}
int main(){
Derived D;
D.write_a(3);
cout << D.read_a() << endl;
}
The program prints '3'. Ideally, object D should have only 'b' and 'c' as its data variables. But, from the program it appears that it is storing 'a' too. How is the memory organized for the derived class object D?