Default Constructors

 Default constructor:

A constructor which does not accept any arguments is called default constructor.

It is also known as zero argument constructors.

Default constructors cannot be overloaded.

All objects of a class are initialized to same set of values by the default constructor.

Syntax:

class classname

{

            private:

                        ---------

            pubic:

                        classname( )

                        {

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

                        }

};

Example:

class simple

{

            private:

                        inta,b;

            pubic:

                        simple( )

                        {

                                    a=10;

                                    b=20;

                        }

                        void display()

                        {

                                    cout<<a<<b;

                        }

};

void main()

{

               simple obj1,obj2;

               clrscr();

               ob1.display();

              obj2.display();

              getch();

}

Disadvantages:

When many objects of same class are created, all objects are initialized to same set of values.

It is not possible to initialize different objects with different values in default constructor.

Comments