Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

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.2length 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.2length is initialized to the argument 4.2breadth is initialized to the 8.6 by default.
Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!