Constructors in C++

Filed Under: C++
C++ Constructors

Constructors in C++ or any other language are a member function of a class that is used to initialize the newly created objects.

They have the same name as that of the class and do not return any value.

Working of Constructors:

As soon as the compiler encounters a newly created object, it invokes the constructor of the corresponding class automatically.

Then, the constructor initializes the newly created object of the class.


Types of Constructors in C++

Following are the types of Constructors in C++:

  • Default Constructors
  • Parameterized Constructors
  • Copy Constructors

1. Default Constructors

Defaulter Constructors do not accept any arguments or parameters in it.

This constructor is implicitly invoked by the compiler if not declared by the user explicitly.

Syntax:

class_name()
{
  //code
}

Example:

#include <iostream> 
using namespace std; 

class Display { 
public: 
	int x; 

	// Default Constructor 
	Display() 
	{ 
		 x = 10;
	} 
}; 

int main() 
{ 
	
	Display d; 
	cout << "The value of x: " << d.x << endl ;
	
	return 1; 
} 

Output:

The value of x: 10

2. Parameterized Constructors

Parameterized constructors allowing the passing of parameters during the creation of an object.

It helps in the initialization of the objects and provides distinct values to the different data variables of the objects.

Syntax:

class_name(parameters)
{
   //code
}

Example:

#include <iostream>
using namespace std;
class Multiply{
public:
   //Parameterized constructor
   Multiply(int a, int b) {
       int result = a*b;
     cout<<result<<endl;
   }
};
int main(void){
   
   Multiply m(10, 10); //implicit call to the constructor
   
   Multiply ex = Multiply(100, 5); //explicit call to the constructor
   
   return 0;
}

Output:

100
500

3. Copy Constructors

Copy Constructor creates a copy of an existing object of the corresponding class. Thus, it creates a copy of all the data variables of one object into another object.

Syntax:

class_name(const class_name & object_name)
{
    //code
}

Example:

#include<iostream>
using namespace std;
class Construct
{
   
    int a, b;   
    
    public:
    Construct(int aa, int bb)
    {
        a = aa;
        b = bb;
    }
    
    
    Construct (const Construct &con) // Copy constructor
    { 
        a = con.a;
        b = con.b;
    }
    
    void show ()
    {
        cout<<a<<" "<<b<<endl;
    }
};

int main()
{
    Construct C(5, 5);     
    Construct C1 = C;      // Calling the Copy constructor
    
    cout<<"Copy constructor:\n ";
    C1.show();
    return 0;
}

Output:

Copy constructor:
 5 5

Constructor Overloading in C++

Overloading serves with the same function definition having different numbers and types of arguments.

In a similar fashion of Function Overloading, even the Constructors can be overloaded in C++ language.

Example:

#include<iostream>
using namespace std;
class Area_parameters
{
    public:
    int a, len, width;
   
    Area_parameters(int x)
    {
        a = x;
        cout<<x<<endl;
    }
    
    Area_parameters(int l, int b)
    {
        len = l;
        width = b;
        cout<<l<<" "<<b<<endl;
    }
};

int main()
{
    
   Area_parameters obj(5);
    
   Area_parameters obj1(10, 20);
    
}

Output:

5
10 20

Difference between Constructors and Member Functions

  • Constructor doesn’t provide any return type, on the other hand, Functions do have a return type.
  • Constructor gets implicitly called when an object is created while Member functions need to be called explicitly.

Conclusion

Thus, in this article, we have understood the functionality served by the Constructors in C++.


References

Constructors in C++ Documentation

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages