Constructor Overloading
Constructor overloading is a concept in programming where a class can have multiple constructors, each with a different parameter list. This allows objects of the class to be created in various ways, depending on the arguments provided during object creation.
With constructor overloading, a single class can initialize objects differently depending on the situation or inputs provided. Complex classes benefit most, where objects may require many initialization approaches.
Advantages of Constructors Overloading :
- Flexible Initialization: Varied constructors enable flexible object creation, tailored to specific needs.
- Better Code Quality: Logical overloads can enhance readability and maintainability for easier understanding.
- Use of Default and Copy Constructors: Default constructors (no parameters) and copy constructors (one parameter of the same class type) are special forms of constructor overloading.
#include <iostream>
using namespace std;
class Box {
public:
Box() {
// default constructor
cout << "Default Constructor called!" << endl;
length = 5; // default value
}
Box(double l) {
// parameterized constructor
cout << "Parameterized Constructor called!" << endl;
length = l;
}
Box(const Box &obj) {
// copy constructor
cout << "Copy Constructor called!" << endl;
length = obj.length;
}
double getLength() {
return length;
}
private:
double length;
};
int main() {
Box Box1; // invokes default constructor
Box Box2(10.0); // invokes parameterized constructor
Box Box3(Box2); // invokes copy constructor
cout << "Length of Box1 : " << Box1.getLength() << endl;
cout << "Length of Box2 : " << Box2.getLength() << endl;
cout << "Length of Box3 : " << Box3.getLength() << endl;
return 0;
}
public class Box {
private double length;
public Box() {
// Default constructor
System.out.println("Default Constructor called!");
this.length = 5; // Default value
}
public Box(double l) {
// Parameterized constructor
System.out.println("Parameterized Constructor called!");
this.length = l;
}
public Box(Box obj) {
// Copy constructor
System.out.println("Copy Constructor called!");
this.length = obj.length;
}
public double getLength() {
return this.length;
}
public static void main(String[] args) {
Box box1 = new Box(); // Invokes default constructor
Box box2 = new Box(10.0); // Invokes parameterized constructor
Box box3 = new Box(box2); // Invokes copy constructor
System.out.println("Length of Box1 : " + box1.getLength());
System.out.println("Length of Box2 : " + box2.getLength());
System.out.println("Length of Box3 : " + box3.getLength());
}
}
class Box:
def __init__(self, length=5):
"""
Default and parameterized constructor.
If no argument is provided, it acts as a default constructor.
If an argument is provided, it acts as a parameterized constructor.
"""
if length == 5:
print("Default Constructor called!")
else:
print("Parameterized Constructor called!")
self.length = length
def __copy__(self):
"""
Copy constructor.
"""
print("Copy Constructor called!")
return Box(self.length)
def get_length(self):
"""
Function to get the length of the box.
"""
return self.length
# Main function
if __name__ == "__main__":
Box1 = Box() # invokes default constructor
Box2 = Box(10.0) # invokes parameterized constructor
Box3 = Box2.__copy__() # invokes copy constructor
print(f"Length of Box1 : {Box1.get_length()}")
print(f"Length of Box2 : {Box2.get_length()}")
print(f"Length of Box3 : {Box3.get_length()}")
# Note: Constructor overloading is simulated in Python by defining multiple __init__
# methods with different signatures or default arguments. In this example,
# the __init__ method acts as both a default and a parameterized constructor,
# depending on whether an argument is provided or not.
class Box {
// Default constructor
constructor(length = 5) {
if (length === 5) {
console.log("Default Constructor called!");
} else {
console.log("Parameterized Constructor called!");
}
this.length = length;
}
// Copy constructor
static copy(box) {
console.log("Copy Constructor called!");
return new Box(box.length);
}
// Method to get the length
getLength() {
return this.length;
}
}
// Main function
function main() {
let Box1 = new Box(); // invokes default constructor
let Box2 = new Box(10.0); // invokes parameterized constructor
let Box3 = Box.copy(Box2); // invokes copy constructor
console.log("Length of Box1 : " + Box1.getLength());
console.log("Length of Box2 : " + Box2.getLength());
console.log("Length of Box3 : " + Box3.getLength());
}
main();
Output
Default Constructor called! Parameterized Constructor called! Copy Constructor called! Length of Box1 : 5 Length of Box2 : 10 Length of Box3 : 10
Function Overloading in Programming
Function Overloading in programming allows multiple functions to have the same name but with different parameters. It lets a function perform different tasks based on the input parameters. This increases the flexibility and readability of the code. Languages like C++, Java, and C# have function overloading. A programmer can provide different implementations for one function name. The number and types of parameters differentiate them. This enhances code readability and usability. Conceptually similar tasks are handled by function variants.
Table of Content
- What is Function Overloading?
- How Function Overloading Works?
- Implementation of Function Overloading
- Constructors and Destructors Overloading
- Constructor Overloading
- Destructor Overloading:
- Operator Overloading:
- Function Overloading in C
- Function Overloading in C++
- Function Overloading in Java
- Function Overloading in Python
- Advantages of Function Overloading
- Disadvantages of Function Overloading
- Use cases of Function Overloading