Tech talks
So we were in Radhika coffe shop, Fegursson college road, and dicussing technical stuff. Some interview like questions
Questions:
#include
class A
{
virtual void foo();
};
class B: public A
{
void foo();
virtual void foo2();
};
class C: public A
{
void foo();
virtual void foo2();
};
class D: public B, public C
{
void foo();
virtual void foo2();
};
void main()
{
printf("Size A: %d\n",sizeof(A));
printf("Size B: %d\n",sizeof(B));
printf("Size C: %d\n",sizeof(C));
printf("Size D: %d\n",sizeof(D));
}
--------------------------
Output (Compiled on VC++ [Windows]. Compiler: cl.exe):
Size A: 4
Size B: 4
Size C: 4
Size D: 8
Questions:
- What is the size of an empty class?
Ans. Class size is 1 - Class A defines a virtual function foo(). What will be size of this class?
Ans. Class size is 4 (4 bytes are reserve for the virtual pointer) - Class B inherits Class A? Class A defines a virtual function foo()? What will be size of this inherited Class B?
Ans. Class size is 4 (4 bytes are reserve for the virtual pointer) - Class B and C inherit A. D inherits B and C. Class A defines a virtual function foo().What is the size of Class D
Ans. Class size is 8.
#include
class A
{
virtual void foo();
};
class B: public A
{
void foo();
virtual void foo2();
};
class C: public A
{
void foo();
virtual void foo2();
};
class D: public B, public C
{
void foo();
virtual void foo2();
};
void main()
{
printf("Size A: %d\n",sizeof(A));
printf("Size B: %d\n",sizeof(B));
printf("Size C: %d\n",sizeof(C));
printf("Size D: %d\n",sizeof(D));
}
--------------------------
Output (Compiled on VC++ [Windows]. Compiler: cl.exe):
Size A: 4
Size B: 4
Size C: 4
Size D: 8
0 Comments:
Post a Comment
<< Home