DIGIT-ARC read-only archive · snapshot: July 23, 2026

a simple OOP doubt

4 posts · originally from https://geek.digit.in/community/threads/a-simple-oop-doubt.138471/

nims11
nims11 Original post #1
Mar 25, 2011 at 6:47 PM
please clear my doubt
if a class A inherits from B, then why would the private members of the base class B will contribute to the size of the object of derived class A?


P.S.- C.Sc board exam on 30th!!
Liverpool_fan
Mar 25, 2011 at 7:19 PM
It's because the private members do exist in the memory, and are actually there with the class and they are only inaccessible to the derived class members and objects. The public base methods which are inherited WILL be able to access, modify the base members.
It could very well be that you are modifying the private members through the public methods of the class. For instance:

Code:
class B    {
   private:
       int var;
   public:
       void modify_var(int v)    {
           var = v;
        }
};


class A : public B   {
   private:
        int num;
   public:
       void modify_A_var(int v, int n) {
[B]           modify_var(v);[/B]
           num = n;
        }
};

You can see in modify_A_var() in class A is accessing a private member of class A only albeit through a member function of the base class.

P.S.: My C++ is rusty, not programmed for over a year, feel free to correct if my post is wrong or has errors.
P.P.S.: Best of luck for the exam
nims11
nims11 #4
Mar 25, 2011 at 9:25 PM
Thanx a lot LFC_fan.

i played with the code and now everything is clear!! my appreciation for OOP has increased after understanding this...