SIZE OF DERIVED CLASS....

HI GUYS
I have a doubt regarding the size of the derived class.
PROBLEM.....
Let A be a base class of size 12 bytes and B be one of it's derived class.Let B's size be 10 bytes.Let an object ob of type B be created
[INHERITANCE]

B ob; // creating an object of type B

WHAT WOULD BE THE SIZE OF ob???
Last edited on
See for yourself.

1
2
cout << sizeof (B); // or
cout << sizeof ob; // parentheses only needed for types 
@ Catfish4
Thanks Dude :) really thank-you very much.Nice tip 
Last edited on
To tell you the truth I thought so(derived class would have a less size than its base class)I didint't took into consideration the fact that the public member along with the protected members will also be accessible to the derived class but that's not what I asked ::::
1
2
3
4
5
6
7
8
9
10

class a
{
char nkh[12];   // private member......
}
class b:public a 
{
public:
char nakk[10];
}



here nkh is a private member of the base class it cannot be accessed by the derived class anyway...But the size of derived class is still 22(I checked it out thanks Catfish4).Why is this so.....

nb::I am new to this topic.PLEASE FORGIVE ME if I am wrong :))
thanks,
CyberDude....



Last edited on
compiler doesn't exclude variables just cuz things are private.
howcome?? that's what I am looking for
Last edited on
here nkh is a private member of the base class it cannot be accessed by the derived class anyway...But the size of derived class is still 22(I checked it out thanks Catfish4).Why is this so.....

This is what protected is for.
There is private, there is public and finally there is protected.

protected is like private except the derived class can access the protected member of the base class.

http://ideone.com/LmGzmu

Also just because in your original example you can't access the private member, it doesn't mean it's not there. It is there.

I suggest reading more about inheritance.

http://www.cplusplus.com/doc/tutorial/inheritance/#inheritance
If you lock something in a safe, that doesn't mean it disappears. It's still there, it's just not accessible.
Because you can have friend function which have access to private part. Or you could do some pointer magic and access it anyway. Or it can hold some magic number used to find it in binary stream. Or something else.
@Catfish4 thanks again for the nice link dude
@cire thanks for cool example and
@MiiNiPaa thanks for the explanation ....

Got it now. I am new to inheritance nyway thanks guys......
You can make sure that a derived class can not have a size less than the size of its base class executing this simple statement for your example

1
2
std::cout << "sizeof( a ) = " << sizeof( a ) << ", sizeof( b ) = " << sizeof( b )
              << std::endl; 


Any object of a derived class contains all members of its base class. Moreover even when a member function of a base class is private nevertheless it is considered in the function overload resolution for the derived class (if of course its name was placed in the derived class scope with a using declaration).
Last edited on
It seems that I am wrong. There is a situation when the size of a base class can be greater than the size of its derived class. It is the situation when the base class is empty that is when it has no data members or virtual functions. In this case if I am not mistaken the size of the base class can be equal to any non-zero value. IF a derived class contains one data member that has the size that is less than the size of th base class then the size of the derived class itself can be less than the size of the base class. For example empty base class can have size equal to 8 while its derived class with one member of type int can have size equal to 4.
Last edited on
@vlad from moscow::Thanks dude really it helped a lot.But really I did't report u some one else did :)
Topic archived. No new replies allowed.