Wednesday 22 February 2012

Define what is constructor? | Constructors in C++

A constructor is a method that is called when a new instance of an object of a class is created. It initializes all class members whenever you create an object of the class. It is a member function with the same name as its class. It may or may not take any parameters. It does not have any return type. For example, the compiler embeds a call to the constructor for each object when it is created. Suppose a class Z has been declared as follows:
class Z
{
public:
Z(); //Constructor for class Z
};
We cannot declare a constructor as virtual or static, nor we can declare a constructor as const, volatile, or const volatile.
The short form of constructor is ctor.