Constructor Overloading in C++

Constructor Overloading in C++

In the C++ programming language, constructor method overloading has the advantage of defining and declaring multiple constructor methods with the same name but multiple individual class variable parameter lists within a single user-defined class. This allows C++ users to create objects from individual methods based on the existing class arguments provided when creating the required class object.

Constructor Overloading in C++

Similar to function overloading in a class, the constructor overloading concept allows C++ users to define multiple individual initialization behaviors for constructor methods defined in an existing class, depending on how the class object is instantiated.

Special features of constructor overloading method.

  • Same name – All overloaded constructor methods defined in a class must have the same name as the class name.
  • Different parameter lists – The parameter variable list (number, data type, or default order of defined variable parameters) of an overloaded class constructor in a user-defined class must be individual or separate.
  • Default Constructor – If the user does not provide any constructor methods in the current program, the C++ compiler provides a default constructor method. If the C++ user provides an overloaded constructor method, the C++ compiler does not create a default constructor unless it is explicitly defined in the order specified.
  • Constructor Resolution – When creating an object in a class, a proper constructor method is selected based on the number and data type of variable parameter arguments passed.

Why use constructor overloading method in C++?

  • Flexibility – Constructor overloading allows multiple class objects of the same class to be initialized using individual methods within an existing class. This provides C++ programmers with flexibility in program maintenance.
  • Code Readability – Constructor overloading provides C++ programmers with a clear order and simple methods for creating class objects with multiple separate initialization requirements.
  • Convenience – Constructor overloading reduces the need for C++ programmers to create multiple class or factory methods for multiple separate class object parameter initialization scenarios.

Syntax of constructor method overloading.

class ClassName {

public:

ClassName(); // Define the default class constructor method

ClassName(int p); // This is a parameterized constructor method with a single parameter

ClassName(int p, double q); // This is a parameterized constructor method with two parameters in the current class.

// Elements of another member class function

};

Example of constructor method overloading in C++ programming.

So, let’s understand this with an example in C++ programming. In which a constructor overloading method is represented.

#include <iostream>

using namespace std;

class Triangle {

private:

double base;

double height;

public:

// Here we define a Default constructor method

Triangle() {

base = 2.0;

height = 2.0;

cout << “Here we called a Default constructor method.” << endl;

}

// Here we declare a Constructor with one parameter argument

Triangle(double b) {

base = b;

height = 2.0; // here we set Default height for single parameter

cout << “Here we called a Constructor with one parameter.” <<endl;

}

// here we declare a Constructor with two parameters argument

Triangle(double b, double h) {

base = b;

height=h;

cout << “Here we called a Constructor with two parameters.” <<endl;

}

// here we declare a Function to display the each triangle dimensions

void preview() {

cout << “Triangle Base – ” << base << “, Triangle Height – ” << height << endl;

}

// here we declare a Function to calculate and display the area of ​​triangle

void area() {

cout << “Area of ​​triangle – ” << 0.5 * base * height << endl;

}

};

int main() {

// here we are using the default constructor method

triangle tri1;

tri1.preview();

tri1.area();

// here we are using the constructor with one parameter argument method

Triangle tri2(7.0);

tri2.preview();

tri2.area();

// here we are using the constructor with two parameters argument method

Triangle tri3(3.0, 7.0);

tri3.preview();

tri3.area();

return 0;

}

Explanation of a constructor method overloading in C++.

  • Default constructor (Triangle()) – This constructor method is called in the current program when no arguments are passed to the current class. It initializes both the base and height of the triangle to the value 2.0.
  • Single parameterized constructor method Triangle(double b) – This constructor method is called in the current program when one argument (base) is passed. It sets base to the passed value and height to the default value of 2.0.
  • Two parameterized constructor (Triangle(double b, double h)) – This constructor method is called in the current program when two arguments (base and height) are passed to the constructor method. It initializes both base and height to the passed values ​​in the current program.

Leave a Reply