Samsung Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
On my machine sizeof(int) gives 4 .
and sizeof(x) is also coming 4 . So , in this case doesn't it have state behaviour and identity ? .
Also , when i add a double to x , sizeof(x) becomes 16. ( sizeof(double) is giving 8 )
??
If size of state and behaviour is increasing then they shouldn't be zero when only int is there.
#include<iostream>
using namespace std;
class z
{
};
class x
{
int a;
};
int main()
{
std::cout << sizeof(x) << '\n';
std::cout << sizeof(z) << '\n';
}
Run and check, you will get answers as 4, 1 as said before.
output - 4,1
- pramod August 26, 2012reason -
Class without any data members and member function such type of class is known as empty class. Size of object of empty class is always 1 byte.
When we create object of any class at that time object always gets 3 characteristics i.e.
State
Behaviour
Identity
When we create object of empty class at that time State of that object is nothing. Behaviour of that object is also nothing, but compiler assigns a unique address to that object. Memory in Computer is always organized in the form of bytes and minimum memory available at object address location is 1 byte. That's why size of object of empty class is 1 byte.