Constructors

 

A constructor is a special member function used to initialize the objects of its class.

Syntax:

class classname

{

            private:

                        ---------

            pubic:

                        classname(argument list)

                        {

                                    ------------

                        }

};

Characteristics of constructors:

1.    Name of the constructor is same as class name.

2.    There is no return type for constructors.

3.    Constructor should be declared in the public section.

4.    Constructors are executed automatically when an object is created.

5.    A class can have more than one constructor.

6.    Constructors can be overloaded.

7.    If class has more than one constructor, the constructors must have different parameters.

8.    It is not possible to refer to the address of the constructor.

 

Types of constructors:

·       Default constructor

·       Parameterized constructor

·       Copy constructor

Comments