Constructor Overloading in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The use of multiple constructors in the same class is known as Constructor Overloading.

The constructor must follow one or both of the two rules below.

  • All the constructors in the class should have a different number of parameters.
  • It is also allowed in a class to have constructors with the same number of parameters and different data types.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Declaration

Here is the code syntax for the declaration of constructor overloading in C++:

Examples of Constructor Overloading in C++

Here are some examples of Constructor Overloading in C++:

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Example 1

Here is a program to overload two constructors, one to set the name and age of a student with no parameters, and the second to set the name and age of a student with a two-parameter.

Output:

Explanation

In the above C++ program, we have created a class Student with two variables, Name and Age. We have also defined two constructors, Student() and Student(string str, int x). The first constructor is called when the object stu1 is created because we have not passed any argument. The second constructor is called when the object stu2 is created because we have passed two arguments with it. The get_Name() and get_Age() functions return the Name and Age, which we use to print the name and age of stu1 and stu2.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Example 2:

Here is a program to overload three constructors, one to set the area with no parameters, the second to set the area with one parameter, and the third to set the area with two parameters.

Output

Explanation

In the above C++ program, we have created a class Area with one variable, area. We have also defined three constructors, Area(), Area(int side), and Area(int length, int width).

  • The first constructor, Area(), is called when the object obj1 is created because we have not passed any argument.
  • The second constructor, Area(int side), is called when the object obj2 is created because we have passed one argument.
  • The third constructor, Area(int length, int width), is called when the object obj3 is created because we have passed two arguments with it.

How Does Constructor Overloading Work in C++?

Here we are taking an example to understand the working of constructor overloading in C++:

Output

Explanation

Here in the above program, there are two constructors of class Calculate:

  • A default constructor (with no parameters)
  • The constructor with three-parameter

Two objects are created in the main() function.

  1. obj1: The obj1 will automatically call the default constructor without any parameters when it is created. This is due to the reason that no parameters are passed when creating the object. Thus, it complies with the definition of first(Default constructor).
  2. obj2: The obj2 automatically invokes the constructor with three parameters when it is created. This is because only three parameters are passed when creating the object. So, it matches the definition of the second constructor.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Conclusion

  • Constructor overloading can be defined as having multiple constructors with different parameters so that every constructor can perform a different task.
  • The constructor that does not take any argument is called the default constructor.
  • A constructor that has parameters is known as a parameterized constructor.
  • A copy constructor is a member function that initializes an object using another object of the same class.
  • The advantage of constructer overloading is it provides flexibility in creating multiple types of objects for a class.

See Also