Constructors in C++

In this C++ tutorial, we are going to discuss the constructors. Suppose you have to initialize thousand of values of a data member with some initial value, so doing such 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 which initializes objects of a class is a constructor. It is an instance method that usually has the same name as 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 is a constructor by its name and return type.

Properties of Constructor

  • Constructor has same name as class .
  • 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 constructor in a class

#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
 }



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.



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
 }

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

C++ code to implement 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
 }

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 *