
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Constructor Overloading in C++
As we know function overloading is one of the core feature of the object oriented languages. We can use the same name of the functions; whose parameter sets are different. Here we will see how to overload the constructors of C++ classes. The constructor overloading has few important concepts.
- Overloaded constructors must have the same name and different number of arguments
- The constructor is called based on the number and types of the arguments are passed.
- We have to pass the argument while creating objects, otherwise the constructor cannot understand which constructor will be called.
Example
#include <iostream> using namespace std; class Rect{ private: int area; public: Rect(){ area = 0; } Rect(int a, int b){ area = a * b; } void display(){ cout << "The area is: " << area << endl; } }; main(){ Rect r1; Rect r2(2, 6); r1.display(); r2.display(); }
Output
The area is: 0 The area is: 12
- Related Articles
- Constructor overloading in Java
- Constructor Overloading in C#
- Constructor Overloading In Java programming
- Constructor overloading in enumerations in Java.
- What is constructor overloading in Java?
- Overloading in C#
- Overloading in java programming
- Overloading Operators in Python
- Method overloading in Java
- PHP Overloading
- Base Overloading Methods in Python
- Overloading unary operators + in C++
- Overloading unary operator in C++?
- What is overloading in C#?
- Using Method Overloading in Java

Advertisements