1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

problem in calculating size of derived classes

Discussion in 'C++' started by nishu, Apr 14, 2008.

  1. nishu

    nishu New Member

    Joined:
    Apr 9, 2008
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    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?
     
    Last edited by a moderator: Apr 14, 2008

Share This Page