Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   problem in calculating size of derived classes (http://www.go4expert.com/forums/calculating-size-derived-classes-t10011/)

nishu 14Apr2008 14:57

problem in calculating size of derived classes
 
Code:

class base
  {
    private :
                int b;
  };
class derived : public base
    {
    private :
                int j;
    };
void main()
{
cout<<endl<<sizeof(derived)<<endl<<sizeof(base);
}

Output:
4
2
I think size of derived=sizeof(j)+sizeof(b).
Am I right or wrong?If I am wrong then please tell me how the size of derived class is
calculated.If I am right then tell me
Why the sizeof derived class is 4 when it is not inheriting the private member b of class base?


All times are GMT +5.5. The time now is 07:30.