1️⃣ What is Constructor Overloading?
Constructor Overloading in C++ allows multiple constructors in a class with different parameters. The correct constructor is called based on the arguments passed while creating an object.
Key Points:
✅ Multiple constructors in the same class.
✅ Each constructor must have a different parameter list.
✅ C++ automatically decides which constructor to call based on the number and type of arguments.
2️⃣ Example: Constructor Overloading
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int year;
// Default Constructor
Car() {
brand = "Unknown";
year = 0;
}
// Parameterized Constructor with one argument
Car(string b) {
brand = b;
year = 2023;
}
// Parameterized Constructor with two arguments
Car(string b, int y) {
brand = b;
year = y;
}
void display() {
cout << "Car Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
Car car1; // Calls Default Constructor
Car car2("Toyota"); // Calls Constructor with one argument
Car car3("Honda", 2022); // Calls Constructor with two arguments
car1.display();
car2.display();
car3.display();
return 0;
}
Output:
Car Brand: Unknown, Year: 0
Car Brand: Toyota, Year: 2023
Car Brand: Honda, Year: 2022
Explanation:
Car car1; → Calls the default constructor.
Car car2("Toyota"); → Calls the constructor with one argument.
Car car3("Honda", 2022); → Calls the constructor with two arguments.
3️⃣ Constructor Overloading with Different Data Types
#include <iostream>
using namespace std;
class Rectangle {
public:
int width, height;
// Default Constructor
Rectangle() {
width = height = 1;
}
// Constructor with one integer argument
Rectangle(int side) {
width = height = side;
}
// Constructor with two integer arguments
Rectangle(int w, int h) {
width = w;
height = h;
}
int area() {
return width * height;
}
};
int main() {
Rectangle r1; // Calls Default Constructor
Rectangle r2(5); // Calls Constructor with one argument
Rectangle r3(4, 6); // Calls Constructor with two arguments
cout << "Area of r1: " << r1.area() << endl;
cout << "Area of r2: " << r2.area() << endl;
cout << "Area of r3: " << r3.area() << endl;
return 0;
}
Output:
Area of r1: 1
Area of r2: 25
Area of r3: 24
Explanation:
Rectangle r1; → Uses the default constructor (1×1).
Rectangle r2(5); → Creates a square (5×5).
Rectangle r3(4, 6); → Creates a rectangle (4×6).
4️⃣ Constructor Overloading with Default Arguments
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
// Constructor with default argument
Student(string n, int a = 18) {
name = n;
age = a;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Student s1("Alice"); // Uses default age = 18
Student s2("Bob", 22); // Uses given age
s1.display();
s2.display();
return 0;
}
Output:
Name: Alice, Age: 18
Name: Bob, Age: 22
Explanation:
If age is not provided, it defaults to 18.
Student s1("Alice"); → Uses default age 18.
Student s2("Bob", 22); → Uses provided age 22.
5️⃣ Constructor Overloading and Copy Constructor
#include <iostream>
using namespace std;
class Person {
public:
string name;
int age;
// Default Constructor
Person() {
name = "Unknown";
age = 0;
}
// Parameterized Constructor
Person(string n, int a) {
name = n;
age = a;
}
// Copy Constructor
Person(const Person &p) {
name = p.name;
age = p.age;
cout << "Copy Constructor Called!" << endl;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Person p1("Alice", 25); // Calls Parameterized Constructor
Person p2 = p1; // Calls Copy Constructor
p1.display();
p2.display();
return 0;
}
Output:
Copy Constructor Called!
Name: Alice, Age: 25
Name: Alice, Age: 25
Explanation:
p2 = p1; invokes the copy constructor to duplicate p1.
6️⃣ Summary
7️⃣ When to Use Constructor Overloading?
✅ When different ways of initializing objects are needed.
✅ When providing default values while allowing customization.
✅ When working with classes that require flexible initialization.
Would you like to learn about Destructors next? 🚀
0 Comments