Loading...
C++ Constructor Overloading

C++ Constructor Overloading

In this tutorial, we will learn about how to demonstrate the concept of Constructor Overloading in C++ with the help of examples.

Constructor Overloading

Before you learn about Constructor Overloading, make sure you know about C++ Constructors.

In C++, We can have more than one constructor in a class with same name, as long as each has a different list of arguments. Constructors can be overloaded in a similar way as function overloading.

Defining more than one constructor within class by changing the Number of parameters, Types of parameters, and Order of parameters is called Constructor Overloading in C++.

Constructor is overloaded in order to extend functionality of existing constructor.


Key Points of Constructor Overloading

  • Overloaded constructors essentially have the same name (name of the class) and different number of arguments.
  • A constructor is called depending upon the number and type of arguments passed.
  • While creating the object, arguments must be passed to let compiler know, which constructor needs to be called.

Example 1: C++ program to demonstrate Constructor overloading

#include <iostream>
using namespace std;

class Student {
    private:
     int age;
 
    public:
     // 1. Constructor with no arguments
     Student() {
         age = 12;
     }
 
     // 2. Constructor with an argument
     Student(int a) {
         age = a;
     }
 
     int getAge() {
         return age;
     }
 };
 
int main() {
     Student student1, student2(15);
 
     cout << "The Age of 1st Student= " << student1.getAge() << endl;
     cout << "The Age of 2nd Student= " << student2.getAge() << endl;
 
     return 0;
}

Output

The Age of 1st Student = 12
The Age of 2st Student = 15

In this program, we have created a class Student that has a single variable age.

We have also defined two constructors Student() and Student(int a):

  • When the object student1 is created, the first constructor is called because we have not passed any argument. This constructor initializes age to 12.
  • When student2 is created, the second constructor is called since we have passed 15 as an argument. This constructor initializes age to 15.

The function getAge() returns the value of age, and we use it to print the age of student1 and student2.


Example 2: C++ program to demonstrate Constructor overloading

To print the content of a void pointer, we use the static_cast operator. It converts the pointer from void* type to the respective data type of the address the pointer is storing:

#include <iostream>
using namespace std;

class Box {
   private:
    double length;
    double breadth;

   public:
    // 1. Constructor with no arguments
    Box() {
        length = 5.3;
        breadth = 6.4;
    }

    // 2. Constructor with two arguments
    Box(double l, double b) {
        length = l;
        breadth = b;
    }
    // 3. Constructor with one argument
    Box(double len) {
        length = len;
        breadth = 8.6;
    }

    double calculateArea() {
        return length * breadth;
    }
};

int main() {
    Box box1, box2(5.0, 4.2), box3(5.6);

    cout << "When no argument is passed: " << endl;
    cout << "Area of box = " << box1.calculateArea() << endl;

    cout << "\nWhen (5.0, 4.2) is passed." << endl;
    cout << "Area of box = " << box2.calculateArea() << endl;

    cout << "\nWhen breadth is fixed to 8.6 and (4.2) is passed:" << endl;
    cout << "Area of box = " << box3.calculateArea() << endl;

    return 0;
}

Output

When no argument is passed: 
Area of box = 33.92

When (5.0, 4.2) is passed.
Area of box = 21

When breadth is fixed to 8.6 and (4.2) is passed:
Area of box = 48.16
  • When box1 is created, the first constructor is called. length is initialized to 5.3 and breadth is initialized to 6.4.
  • When box2 is created, the second constructor is called. We have passed the arguments 5.0 and 4.2. length is initialized to the first argument 5.0 and breadth is initialized to 4.2.
  • When box3 is created, the third constructor is called. We have passed one argument 4.2. length is initialized to the argument 4.2. breadth is initialized to the 8.6 by default.

Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Constructor Overloading in C++.

Keep Learning : )

In the next tutorial, you'll learn about C++ Object & Function.

- Related Topics