0
class Base 
{
   int i ;
};

class Derived : public Base { } ;

int main()
{
   cout << sizeof(Derived);
}

Output :
 4

In Base class , since i is private , it should not be inherited to the Derived Class . Hence , the Base class should act as empty class . And sizeof empty class is 1 , then why it is showing as 4 ?

taocp
  • 23,276
  • 10
  • 49
  • 62
Subbu
  • 2,063
  • 4
  • 29
  • 42

3 Answers3

4

A Derived class still contains i in Base, it just isn't accessible, so the size 4 is because of i

cdmh
  • 3,294
  • 2
  • 26
  • 41
0

The size still is 4 because the size of Base is 4. Lets say for example that Base has a function public: int getI() {return i;} then you are able to call Derived::getI(), therefore Derived does need to include the private base members.

-2

here,derived class is defined publicly.. means here in definition ": public base"

thus derived class can access all public as well as private members of base class..it will make all private members public in that derived class..thus output is 4 coz of i.not 1.if u write private instead then it will give output 1..

CMDCS
  • 1