2

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?

learner1
  • 41
  • 1
  • 1
    inheritance is an is-a relationship. That means a `Derived` is a `Base` and contains a `Base` as part of itself. – NathanOliver Dec 18 '18 at 17:34
  • 2
    related/dupe: https://stackoverflow.com/questions/8418471/does-a-derived-class-object-contain-a-base-class-object – NathanOliver Dec 18 '18 at 17:36
  • 2
    Possible duplicate of [Does a derived class object contain a base class object?](https://stackoverflow.com/questions/8418471/does-a-derived-class-object-contain-a-base-class-object) – Matthieu Brucher Dec 18 '18 at 17:38
  • 1
    I think you have 2 questions going here: 1. How does the C++ compiler layout memory for inherited classes. There is a SO question and answer here: https://stackoverflow.com/questions/8672218/memory-layout-of-inherited-class. 2. How is it that class `Derived` can access private member Base::a ? Regarding your second question: Derived does contain Base::a, but Derived cannot access it directly. It only accesses it through the Base public member functions - just like you observed. You 'Ideally' statement is in conflict with C++ inheritance. – natersoz Dec 18 '18 at 17:38
  • "Ideally, object D should have only 'b' and 'c' as its data variables". Suppose a is removed from D. What would the last line print then, and how would that be achieved? – n. m. could be an AI Dec 19 '18 at 06:34

1 Answers1

1

object D should have only 'b' and 'c' as its data variables

No. Inheritance is created just to prevent coder from code repetition, for example you have some piece of code and that code should be in two different classes, then you make a Base class and put that piece of code into the base class instead of writing the same code two times. So, a derived class contains the base class.

it appears that it is storing 'a' too. How is the memory organized for the derived class object D?

Yes Derived class is storing a too and naturally a occupies some memory ( because a is an int, its size depends on your system architecture) however that portion of memory is not accessible by Derived class. This is where protected keyword comes in.

eneski
  • 1,575
  • 17
  • 40