Constructors in C++

In this C++ tutorial, we are going to discuss the constructors with examples.

Suppose you have to initialize thousands of values of a data member with some initial value. But doing that kind of task manually is not easy. In OOPS we use constructors in a class to initialize data members with some initial value. It can also be used to declare run-time memory to data members.

What is Constructor?

A special member function of a class in object-oriented programming that initializes objects of a class is a constructor. It is an instance method that usually has the same name as the class and use to set values of a member of an object to default or a user-defined value. It gets called automatically when an object is created. A compiler identifies a given member function as a constructor by its name and return type.

Properties of Constructor

  • A constructor has the same name as the class in C++.
  • A constructor does not have a return type.
  • It gets called automatically called when an object is created.
  • Constructors cannot be inherited.
  • Constructors can be declared using any access modifiers.

C++ code to implement a constructor in a class

Below is given a program as an example to implement a constructor:

#include<bits/stdc++.h>

  using namespace std;

   class codespeedy
 {
     private :
       
          int x;
       
       public :
         
            codespeedy() // declaring constructor 
        {
            x=3;
        }
        
           void show()
        {
            cout<<"Value of x is : "<<x<<endl;
        }
 };

   int main()
 {
     codespeedy c; // object creation
     
      c.show(); // calling data member of class using object
 }



Running the above program will give the result as the output given below:

Value of x is : 3

Some Types of Constructor

1.) Default Constructor 

A constructor which does not take any argument is a default constructor. The default constructor has no parameters.

Below is an example of C++ code to implement default constructor in a class:

#include<bits/stdc++.h>

   using namespace std;

    class codespeedy
 {
     private :
       
        int a;
       
        int b;
       
       public :
         
             codespeedy() // declaring constructor 
        {
            a=1;
            
             b=2;
        }
        
             void show()
        {
            cout<<"Value of a is : "<<a<<endl;
             
            cout<<"Value of b is : "<<b<<endl;
        }
 };

   int main()
 {
     codespeedy c; // object creation
     
      c.show(); // calling data member of class using object
 }

Output:

Value of a is : 1
Value of b is : 2

2.) Parameterized Constructor

It is possible to initialize objects in a class by passing arguments to the constructors. This type of constructor is known as a parameterized constructor. The constructor can be called explicitly or implicitly.

codespeedy c = codespeedy (5,6); // explicit call

codespeedy c (5,6); // Implicit call

Let’s see the C++ code to implement the parameterized constructor:

#include<bits/stdc++.h>

 using namespace std;

    class codespeedy
 {
     private :
       
       int a;
       
         int b;
       
         public :
          
             codespeedy(int x , int y) // declaring constructor 
        {
            a=x;
           
             b=y;
        }
        
            void show()
        {
            cout<<"Value of a is : "<<a<<endl;
            
             cout<<"Value of b is : "<<b<<endl;
        }
 };

   int main()
 {
     codespeedy c(5,6); // Constructor called
     
      c.show(); // calling data member of class using object
 }

Output:

Value of a is : 5
Value of b is : 6

Note:- If a constructor is not specified, then C++ compiler generates a default constructor with no parameters.

Leave a Reply

Your email address will not be published. Required fields are marked *