Introduction of Constructor

 


Constructor:  It is a member function of class that initialize the object of class.

                       It is called automatically when object of class is created.


Types of Constructor

1. Default Constructor

It is constructor without any arguments.

Syntax: class className

             {

                 className( )

                   {

                            }

                                 };

Example:

#include<iostream.h>

#include<conio.h>

class Default

{

  int x,y;

 public:

      Default()    //default constructor

       {

         x=0;

         y=0;

          }

void display()

  {

    cout<<"Value of x="<<x<<endl<<"Value of y ="<<y;

     }

      };

void main()

 {

  Default d;

   d.display();

    getch();

     }

Output:


2.Parameterized Constructor

It is the constructor that have arguments as parameters

Syntax: class className

             {

                   className ( ) //default constructor

                      className(argument1,argument2) //parameterized constructor

                              };

Example:

             #include<iostream.h>

             #include<conio.h>

class Parameter

 {

   int x,y;

  public:

      Parameter()    //default constructor

       {

         x=0;

         y=0;

          }

          Parameter(int a,int b)  //parameterized constructor

            {

            x=a;

            y=b;

              }

           void input()

           {

            cout<<"Enter value of x:";

            cin>>x;

            cout<<"Enter value of y:";

            cin>>y;

              }

  void display()

   {

    cout<<"Value of x="<<x<<endl<<"Value of y ="<<y;

    }

      };

  void main()

      {

       Parameter p;

       p.input();

       p.display();

      getch();

      }

Output:



3.Copy Constructor

It is a constructor which creates  object as a copy of

existing object of same class.

Syntax:

class className

{

  className(className &); //copy constructor use reference(&) of the same class object as argument

   };

Example:#include <iostream.h>

#include <conio.h>


class Copy

 {

   int x,y;

  public:

   Copy();

   Copy(int a,int b);

   Copy(Copy &);

   void input();

   void display();

      };

      Copy::Copy()    //default constructor

       {

         x=0;

         y=0;

          }

          Copy::Copy(int a,int b)  //parameterized constructor

            {

            x=a;

            y=b;

              }

              Copy::Copy(Copy &c) //copy constructor

                {

                  x=c.x;

                  y=c.y;

                     }

       void Copy::input()

           {

            cout<<"Enter value of x:";

            cin>>x;

            cout<<"Enter value of y:";

            cin>>y;

              }

   void Copy::display()

   {

    cout<<"Value of x="<<x<<endl<<"Value of y ="<<y;

    }

    void main()

      {

      Copy c1;

       c1.input();

       c1.display();

       Copy c2(c1);

      getch();

      }



Comments