Wednesday 22 February 2012

Define two methods of constructor invocation? | Constructor in C++

The following are the two ways to invoke a constructor:
1. The compiler embeds a call to the constructor for each object that has got created. For example:
class A
{
int x;
public: void setx(const int=0);
int getx();
};
main()
{
A A1; //object declared constructor called.
A1.A(); //constructor called implicity.
}
2. The constructor is called for each object that is created dynamically in the heap by the new operator. For example:
A *Aptr
A*ptr=new A; //constructor called implicity by compiler.