# Include <iostream>
Using namespace STD;
Class A // A is an empty class. The compiler will mark this class with a char type and the size is 1.
{
};
Class B: public a // B inherits a, but it is also an empty class with the size of 1
{
};
Class C: Virtual
Public A // during virtual inheritance, the compiler inserts a pointer to the parent class with a size of 4
{
};
Class D // The size is 4
{
Public:
Int;
Static int B; // static variables are stored in the static storage area.
};
Class E // print function does not occupy memory space, size is 4
{
Public:
Void print () {cout <"E" <Endl ;}
PRIVATE:
Int;
};
Class F // virtual functions occupy the memory of a pointer. The system needs to use this pointer to maintain the virtual function table. The size is 8.
{
Public:
Virtual void print () {cout <"F" <Endl ;}
PRIVATE:
Int;
};
Class G: Public f // The memory size of one more virtual function remains unchanged. It can be seen that a class has only one virtual function pointer. The size is 8.
{
Public:
Virtual void print () {cout <"G" <Endl ;}
Virtual void print2 () {cout <"G2" <Endl ;}
};
Int main ()
{
A;
B;
C;
D;
E;
F;
G;
Cout <sizeof (a) <"" <sizeof (a) <Endl; // 1
Cout <sizeof (B) <"" <sizeof (B) <Endl; // 1
Cout <sizeof (c) <"" <sizeof (c) <Endl; // 4 4
Cout <sizeof (d) <"" <sizeof (d) <Endl; // 4 4
Cout <sizeof (e) <"" <sizeof (e) <Endl; // 4 4
Cout <sizeof (f) <"" <sizeof (f) <Endl; // 8
Cout <sizeof (g) <"" <sizeof (g) <Endl; // 8
Return 0;
}