S
somenath
I am not able to understand the output of the following program?
#include<iostream>
using namespace std;
class Base{
protected:
int x;
char y;
};
class Derivedublic Base {
protected:
char z;
};
int main(void)
{
cout<<"Sizeof (int) = "<<sizeof(int)<<endl;
cout<<"Sizeof (char) = "<<sizeof(char)<<endl;
cout<<"sizeof(Base) = "<<sizeof(Base)<<endl;
cout<<"sizeof(Derived) = "<<sizeof(Derived)<<endl;
return 0;
}
Output
+++++++++
Sizeof (int) = 4
Sizeof (char) = 1
sizeof(Base) = 8
sizeof(Derived) = 8
I can understand why sizeof(Base) = 8.
It is because sizeof(int) + sizeof(char) + padding = 4 + 1 + 3
But I expect sizeof(Derived) to be 12 because of sizeof(Base) + sizeof(Derived) + padding = 8 + 1 + 3 =12
But why the sizeof(Derived) is 8 ?
#include<iostream>
using namespace std;
class Base{
protected:
int x;
char y;
};
class Derivedublic Base {
protected:
char z;
};
int main(void)
{
cout<<"Sizeof (int) = "<<sizeof(int)<<endl;
cout<<"Sizeof (char) = "<<sizeof(char)<<endl;
cout<<"sizeof(Base) = "<<sizeof(Base)<<endl;
cout<<"sizeof(Derived) = "<<sizeof(Derived)<<endl;
return 0;
}
Output
+++++++++
Sizeof (int) = 4
Sizeof (char) = 1
sizeof(Base) = 8
sizeof(Derived) = 8
I can understand why sizeof(Base) = 8.
It is because sizeof(int) + sizeof(char) + padding = 4 + 1 + 3
But I expect sizeof(Derived) to be 12 because of sizeof(Base) + sizeof(Derived) + padding = 8 + 1 + 3 =12
But why the sizeof(Derived) is 8 ?