a simple OOP doubt

nims11

BIOS Terminator
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

Sami Hyypiä, LFC legend
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
 
OP
nims11

nims11

BIOS Terminator
Thanx a lot LFC_fan.

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