
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Basic Input/Output
- C++ Modifier Types
- C++ Storage Classes
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Return Values
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Pointers
- C++ Pointers
- C++ Dereferencing
- C++ Modify Pointers
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ unordered_multiset
C++ - Constructor Overloading
Constructor Overloading
In C++, constructor overloading is a concept in object-oriented programming (OOP), where the user can define multiple constructors with the same name in a class with each having a different parameter list.
Syntax
Here's the syntax for constructor overloading in C++.
class ClassName { public: // Constructor with no parameters (default constructor) ClassName() { // Initialization code } // Constructor with one parameter ClassName(type param1) { // Initialization code using param1 } // Constructor with two parameters ClassName(type param1, type param2) { // Initialization code using param1 and param2 } // Constructor with more parameters if needed ClassName(type param1, type param2, type param3) { // Initialization code using param1, param2, and param3 } };
Example of Constructor Overloading
In this example, we will see how Constructor overloading allows the "Rectangle class" to have multiple constructors, each with a different parameter list. when we create an object of the rectangle class, the compiler automatically chooses the constructor that perfectly fits based on the arguments passed, showing how constructor overloading provides flexibility.
Here is the following example of constructor overloading in C++.
#include <iostream> using namespace std; class Rectangle { public: int length, width; // Default constructor (no parameters) Rectangle() { length = 1; width = 1; } // Constructor with one parameter (square) Rectangle(int side) { length = side; width = side; } // Constructor with two parameters (rectangle) Rectangle(int l, int w) { length = l; width = w; } // Method to display the area of the rectangle void displayArea() { cout << "Area: " << length * width << endl; } }; int main() { // Using different constructors Rectangle rect1; // Default constructor Rectangle rect2(5); // Constructor with one parameter Rectangle rect3(5, 3); // Constructor with two parameters rect1.displayArea(); rect2.displayArea(); rect3.displayArea(); return 0; }
Output
Area: 1 Area: 25 Area: 15
Explanation
- Firstly, we defined a class rectangle, with two public member variables, length and width.
- Then a default constructor Rectangle() with values length and width equal to 1, then Constructors with One Parameter Rectangle(int side) and Two Parameters Rectangle(int l, int w).
- When no arguments passed Rectangle rect1; the default constructor is called initializing length and width to 1.
- When one argument is passed, Rectangle rect2(5); the constructor with one parameter is called, initializing both length and width to 5.
- when two arguments are passed Rectangle rect3(5, 3); the constructor with two parameters is called, initializing length to 5 and width to 3.
Benefits of Constructor Overloading
Constructor Overloading provides various benefits, making it an essential feature for creating flexible and efficient classes.
1. Flexibility in Object Initialization
It gives you multiple ways of initializing an object or Multiple Initialization Options.
2. Cleaner and Readable Code with enhanced Code Maintainability
By providing different ways of initializing an object, it reduces the need for multiple setter methods or complex initialization logic, avoids redundancy, and provides simpler object creation, which ultimately gives cleaner and more readable code and easier to modify.
3. Encapsulation of Initialization Logic
It also encapsulates the initialization logic within the constructor, which means the initialization logic is managed inside the constructor rather than being spread across various methods or outside the class.
4. Simplifies Object Cloning (Copy Constructors)
Constructor overloading allows to defined copy constructor to handle both shallow and deep copying objects, this makes sure that the object is easily copied.