How to use Constructors and their Types in C++

What are Constructors, their Types, and Constructors in C++?

In this tutorial, We will Learn:

What are Constructors?

What are the types of constructors with simple programming examples?

How to use constructors in C++ programming with examples?

Defination of constructor:

Constructors are special class functions which performs initialization of every object.

The Compiler calls the Constructor whenever an object is created.

Constructor’s initialize values to data members after storage is allocated to the object.

While defining a constructor you must remember that the name of constructor will be same as the name of the class, and constructors never have return type.

Constructors can be defined either inside the class definition or outside class definition using class name and scope resolution :: operator.

Video Tutorial on How to use Constructors and their Types in C++

Constructors are of four types :

1. Default Constructor

2. Parameterized Constructor

3. Copy Constructor

4. Explicit Constructor

1. Default Constructor in C++

Default constructor is the constructor which doesn’t take any argument. It has no parameters in default constructors

Syntax :

class_name ()

{

Constructor Definition

}

Following program demonstrates the default constructor:

 #include<iostream>
 using namespace std;
 class areaRect
 {
     private:
         int h, w;
     public:
     areaRect()
     {
         h = 0;
         w = 0;
     }
     int area()
    {
     cout<<"Enter the height of rectangle: ";
     cin>>h;
     cout<<"Enter the width of the rectangle: ";
     cin>>w;
     return h*w;
   }
 };
 int main()
 {
     int result;
     areaRect a1;
     result = a1.area();
     cout <<endl<<"Area of Rectangle is: "<<result;
 }

Output:

Area of Rectangle is: 0

2. Parameterized Constructor in C++

These are the constructors with parameters. Using this Constructor you can provide different values to data members of different objects, by passing the appropriate values as an argument.

Syntax :

class_name ()

{

Constructor Definition

}

Following program demonstrates the parameterized constructor:

 #include<iostream>
 using namespace std;
 class areaRect
 {
     private:  int h, w;
     public:
     areaRect()
     {
         h = 0;
         w = 0;
     }
     areaRect(int x, int y)
     {
         h = x;
         w = y;
     }
     int area()
     {
         return h*w;
     }
 };
 int main()
 {
     int result;
     areaRect a1;
     result = a1.area();
     cout <<endl<<"Area of Rectangle is: "<<result;
     areaRect a2(10, 20);
    result = a2.area();
    cout <<endl<<"Area of Rectangle is: "<<result;
 }    

Output:

Area of Rectangle is: 0

Area of Rectangle is: 200

3. Copy Constructor in C++

Copy constructor is a special type of constructor in which new object is created as a copy of an existing object.

The copy constructor is called whenever a new variable is created from an object.

Syntax :

class_name (class_name &obj)

{

Constructor Definition

}

Following program demonstrates the copy constructor:

 #include<iostream>
 using namespace std;
 class areaRect
 {
     private:  int h, w;
     public:
     areaRect()
     {
         h = 10;
         w = 10;
     }
     areaRect(areaRect &obj)
     {
         h = obj.h;
         w = obj.w;
     }
     int area()
     {
         return h*w;
     }
 };
 int main()
 {
     int result;
     areaRect a1;
     result = a1.area();
     cout <<endl<<"Area of Rectangle is: "<<result;
   areaRect a2(a1);
    result = a2.area(); 
    cout <<endl<<"Area of Rectangle is: "<<result;
 }    

Output:

Area of Rectangle is: 100

Area of Rectangle is: 100

4. Explicit Constructor in C++

In C++, compiler is allowed to make one time type conversion to resolve the parameters to functions.

In C++, a constructor with only one required parameter is considered as an implicit conversion function. It converts the parameter type to the class type.

Prefixing explicit keyword prevents the constructor from using the implicit conversion.

Syntax :

explicit class_name (parameters)

{

Constructor Definition

}

Following program demonstrates the explicit constructor:

 #include<iostream>
 using namespace std;
 class test
 {
     private:   
        int val;
     public: explicit test(int x)
    {
     val = x;
  }
  void display()
  {
        cout <<"The value of val is "<<val;
   }
 };
 int main()
 {
     test t1=10;
     t1.display();
 }

Output:

The value of val is 10.

Summary:

In this article, we understood what are constructors, different types of constructors, and how to use constructors in C++ using simple programming examples.

If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published.