C++ Types Of Constructor

  • Home
  • C++
  • C++ Types Of Constructor

Types of Constructor


There are four types of constructor:
  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor
  4. Dynamic Constructor

Default Constructor


  • Constructor that accepts no argument
  • If class does not have any constructor, the compiler automatically provides a default constructor.

Syntax

class class_name
{
   private/public/protected:
   class_name()
   {
        Statements;
   }
};


Program to show default constructor.



#include<iostream.h>
#include<conio.h>
class data
{
    int a;
    float b;
    public:
    data()
    {
       a=10;
       b=12.4;
    }
    void show()
    {
        cout<<"\n a="<<a;
        cout<<"\n b="<<b;
    }
};
void main()
{
    clrscr();
    data d;
    d.show();
    getch();
}



Output


a=10
b=12.4

Parameterized Constructor


  • Constructor that accepts arguments
  • It allow us to initialize the various data elements of different objects with different values.
  • Default constructor becomes hidden.

Syntax


class class_name
{
     public/protected/private:
     class_name(Argument)
     {
          Statements;
      }
};


Program to show parameterized constructor.



#include<iostream.h>
#include<conio.h>
class data
{
    int a;
    float b;
    public:
    data(int x, float y)
    {
       a=x;
       b=y;
    }
    void show()
    {
        cout<<"\n a="<<a;
        cout<<"\n b="<<b;
    }
};
void main()
{
    clrscr();
    data d(10,12.4);
    d.show();
    getch();
}



Output


a=10
b=12.4

Copy Constructor


  • Constructor which can be used to copy one object to another.
  • Compiler will automatically use copy constructor when you initialize an object with another object.

Syntax


class class_name
{
     public/private/protected:
     class_name(class_name &object)
     {
            Statements;
     }
};


Program to show copy constructor.




#include<iostream.h>
#include<conio.h>
class data
{
    int a;
    float b;
    public:
    data(int x, float y)
    {
       a=x;
       b=y;
    }
    data(data &obj)
    {
         a=obj.x; 
         b=obj.y;
    }
    void show()
    {
        cout<<"\n a="<<a;
        cout<<"\n b="<<b;
    }
};
void main()
{
    clrscr();
    data d1(10,12.4);
    data d2(d1);
    data d3=d1;
    cout<<"\nD1 = ";
    d1.show();
    cout<<"\nD2 = ";
    d2.show();
    cout<<"\nD3 = ";
    d3.show();
    getch();
}


Output


D1=
a = 10
b = 12.4
D2=
a = 10
b = 12.4
D3=
a = 10
b = 12.4

Dynamic Constructor


  • Constructor used to initialize an object with some initial values during the run time.
  • Values of arguments are read during execution and then those values are used to initialize the object.

Program to use dynamic constructor




#include<iostream.h>
#include<conio.h>
class data
{
    int a;
    float b;
    public:
    data(int x, float y)
    {
         a=x;
         b=y;
    }
    void show()
    {
       cout<<"\n a = "<<a;
       cout<<"\n b = "<<b;
    }
};
void main()
{
     clrscr();
     data d;
     int h;
     float i;
     cout<<"\nEnter a = ";
     cin>>h;
     cout<<"\nEnter b = ";
     cin>>i;
     data d(h,i);
     d.show();
     getch();
}


Output


Enter a =14
Enter b = 20.6
a=14
b=20.6

Back To Top