1. Size of empty class is 1, compiler needs to distinguish better the classes.
2.Static variable part of class do not take part in size of the class.
3.Static Integer variables can be initialized in the class declaration
Class XYZ
{
private:
static const int n=10;
};
4.A
static function can not be virtual : Static works on class without
object where as virtual needs and instance to be created and works at
runtime.
5.Avoid calling virtual functions from constructor, as
they behave like non-virtual as the instance of the class/object
creation is not yet completed.
6.Class with private destructor
should be created only on heap : If you create an instance on stack,
compiler will have no clue on how to call destructor as it is private.
7.Reference
and Const variables should be initialized using member initialization
list: This helps the initialization of these member at the tile of
object creation.
8.Compiler generated functions are public and
inline AND they will be created only if they are used some where in the
code else compiler will not generate them.
9.Reference and Const
can not be copied, so compiler will not generate assignment operator and
forces the user to create an assignment operator if the class has const
or reference members.
10. What happens if there is a default constructor in Base class and user tries to initialize using the default constructor.
class Base
{
public:
Base(int a)
{
}
};
class Derived: public Base
{
public:
void display()
{
}
};
Derived obj; //Compiler Error
Reason
: Default Constructor calls base class default constructor and also
default constructor of the members. If any of the default constructor is
missing then compiler throws error.
11.Assignment Operator is member by member copy, where as Copy constructor is member by member initialization.
12.Size of a class with virtual function is SizeOf(Class) + SizeOf(vPtr) = Class + 4
13.Following are NOT inherited from Base Class
- Constructor
- Destructor
- Assignment Operator
- Friends
14.No
Virtual constructors in C++ : C++ is a staitc typed language and needs
to know the type of the object at the time of creation.
Virtual constructor can be achieved using Factory Method Design Pattern.
15.You
should not delete the variables that are created using placement
new.You should delete the complete buffer once its no longer required.
16.Volatile variables are modified by some external system other than the current code. Like,
- CPU clocks
- Interrupt signals
17.Static global variables have only local linkage and are accessible only in the current file.
Where as global(non-static) variables have external linkage and are accessible using extern keyword.
18.When
dynamic_cast is applied on reference variable of a base(Not referring
to derived object) then "bast_cast" exception will be thrown.If it is a
pointer type then NULL will be returned.
19.VTable is created per class , VPtr is created per object.
20. Vptr Address : (int*) (&obj+0)
VTable Address : (int*)*(int*)(&obj+0)
Vptr is vtable starting address.