Constructor: – A constructor is a special member function whose main function is to allocate the required resources such as memory and initialize the object of its class. A constructor is distinct from other member function of the class, and it has the same name as its class. It is executed automatically when a class is initialized. It is generally used to initialized object member parameter and allocate the necessary resources to the object members.

The C++ run time system makes sure that the constructor of a class is the first member function to be executed automatically when an object of the class is created. In other word, the constructor is executed every time an object that class is defined. Normally constructor are used for initializing the class data member. There are four different types of constructer are used in C++ programming. They are called:

  1. Default constructor
  2. Simple constructor (constructor)
  3. Parameterized constructor
  4. Copy constructor

Default constructor: – A constructor initialize internally an object in the memory is called default constructor. This type of constructor create automatically without its name. Previous all object programming are example of default constructor.

Simple constructor (constructor): – When a constructor declare in a class without any argument, is called constructer or simple constructor. Only one constructor created in a class. Syntax of the constructor is: –

Class <class name>
{
Private:
                _ _ _ _ _
                _ _ _ _ _
Public:
                <Class name>()
                {
                Body of constructor
                }
                _ _ _ _ _
                _ _ _ _ _
};
                                Or
Class <class name>
{
Private:
                _ _ _ _ _
Public:
                <Class name>();
                _ _ _ _ _
};
<Class name>::<Class name>()
{
                Body of constructor
}

Parameter constructor: – A constructor can be invoked with argument, just as in the case of function. The argument list can be specified with in a small bracket similar to argument list in the function. Constructor with argument are called parameterized constructor. Number of parameterized constructors can be used in one class. When number of constructor used in a class, is called constructor overloading. When user or programmer use constructor overloading, they must sure that either number of argument, type of argument or both must be different in all constructor. Syntax of the parameterized constructor is: –

Class <class name>
{
Private:
                _ _ _ _ _
Public:
                <Class name>(list of argument)
                {
                                Body of constructor
                }
};
                                Or
Class <class name>
{
Private:
                _ _ _ _ _
Public:
                <Class name>(list of argument);
};
<Class name>::<Class name>(Argument list)
{
                Body of constructor
}

Copy constructor: – When an object of one class can be copy into another class is called copy constructor. The parameter, of a constructor can be of any of the data type accept an object of its own class as value parameter. Such a constructor having a reference to an instance of its own class as an argument and is known as copy constructor.

The complier copies all the members of the user defined source object to the destination object in the assignment statement, when its members are statically allocated. The data members, which are dynamically allocated must be copied to the destination object explicitly. It can be performed by either using the assignment operator or the copy constructor. Syntax of the copy constructor is-

Class <class name>
{
                _ _ _ _ _
                _ _ _ _ _
Public:
                <Class Name>()
                <Class Name>(<Class Name> &OBJ);
};

 

Advertisements