Constructor Overloading In C++

In C++, Constructor overloading means defining multiple constructors in a class with different parameters. it is similar to function overloading.

  • Overloaded constructors have the same name as the class, but they differ in the number and type of arguments they accept.
  • When creating an object, you must pass the correct arguments so the compiler can determine which constructor to invoke.

Constructor Overloading In C++

Example of Constructor Overloading in C++

#include <iostream>
using namespace std;

class Car {
public:
string brand;
int modelYear;

// Default Constructor
Car() {
brand = “Unknown”;
modelYear = 0;
cout << “Default Constructor Called\n”;
}

// Parameterized Constructor
Car(string b, int y) {
brand = b;
modelYear = y;
cout << “Parameterized Constructor Called\n”;
}

// Display function
void display() {
cout << “Brand: ” << brand << “, Model Year: ” << modelYear << endl;
}
};

int main() {
Car car1; // Calls Default Constructor
Car car2(“Toyota”, 2022); // Calls Parameterized Constructor

// Display details
car1.display();
car2.display();

return 0;
}

Output

Default Constructor Called
Parameterized Constructor Called
Brand: Unknown, Model Year: 0
Brand: Toyota, Model Year: 2022

Constructor Overloading In Inheritance (C++)

When a class inherits another class, the constructor of the base class is called before the constructor of the derived class. We can overload constructors in both the base and derived classes.

 

#include <iostream>
using namespace std;

// Base class
class Vehicle {
public:
string type;

// Default Constructor
Vehicle() {
type = “Unknown”;
cout << “Base Default Constructor Called\n”;
}

// Parameterized Constructor
Vehicle(string t) {
type = t;
cout << “Base Parameterized Constructor Called\n”;
}
};

// Derived class
class Car : public Vehicle {
public:
string brand;
int modelYear;

// Default Constructor (Calls Base Default Constructor)
Car() {
brand = “Unknown”;
modelYear = 0;
cout << “Derived Default Constructor Called\n”;
}

// Parameterized Constructor (Calls Base Parameterized Constructor)
Car(string t, string b, int y) : Vehicle(t) {
brand = b;
modelYear = y;
cout << “Derived Parameterized Constructor Called\n”;
}

// Display function
void display() {
cout << “Type: ” << type << “, Brand: ” << brand << “, Model Year: ” << modelYear << endl;
}
};

int main() {
Car car1; // Calls Default Constructors
Car car2(“Sedan”, “Toyota”, 2022); // Calls Parameterized Constructors

// Display details
cout << “Car 1 Details: “; car1.display();
cout << “Car 2 Details: “; car2.display();

return 0;
}

 

output

Base Default Constructor Called
Derived Default Constructor Called
Base Parameterized Constructor Called
Derived Parameterized Constructor Called
Car 1 Details: Type: Unknown, Brand: Unknown, Model Year: 0
Car 2 Details: Type: Sedan, Brand: Toyota, Model Year: 2022

Example 02: Constructor Overloading in Inheritance

#include <iostream>
using namespace std;

// Base class: Vehicle
class Vehicle {
public:
string type;

// 1st Parameterized Constructor
Vehicle(string t) {
type = t;
cout << “Base Class Constructor 1: Vehicle Type Initialized\n”;
}

// 2nd Parameterized Constructor
Vehicle(string t, int wheels) {
type = t;
cout << “Base Class Constructor 2: Vehicle Type & Wheels Count Initialized\n”;
}

// 3rd Parameterized Constructor
Vehicle(string t, int wheels, string fuelType) {
type = t;
cout << “Base Class Constructor 3: Vehicle Type, Wheels & Fuel Type Initialized\n”;
}
};

// Derived class: Car (inherits Vehicle)
class Car : public Vehicle {
public:
string brand;
int modelYear;

// 1st Parameterized Constructor (Calls Base Constructor 1)
Car(string t, string b, int y) : Vehicle(t) {
brand = b;
modelYear = y;
cout << “Derived Class Constructor 1: Car Brand & Year Initialized\n”;
}

// 2nd Parameterized Constructor (Calls Base Constructor 2)
Car(string t, int wheels, string b, int y) : Vehicle(t, wheels) {
brand = b;
modelYear = y;
cout << “Derived Class Constructor 2: Car Brand, Year, Wheels Initialized\n”;
}

// 3rd Parameterized Constructor (Calls Base Constructor 3)
Car(string t, int wheels, string fuelType, string b, int y) : Vehicle(t, wheels, fuelType) {
brand = b;
modelYear = y;
cout << “Derived Class Constructor 3: Car Brand, Year, Wheels & Fuel Type Initialized\n”;
}

// Display function
void display() {
cout << “Type: ” << type << “, Brand: ” << brand << “, Model Year: ” << modelYear << endl;
}
};

int main() {
cout << “Creating car1 (Using Constructor 1):\n”;
Car car1(“Sedan”, “Toyota”, 2022);

cout << “\n—————————–\n”;

cout << “Creating car2 (Using Constructor 2):\n”;
Car car2(“SUV”, 4, “Ford”, 2021);

cout << “\n—————————–\n”;

cout << “Creating car3 (Using Constructor 3):\n”;
Car car3(“Truck”, 6, “Diesel”, “Volvo”, 2020);

cout << “\nCar 1 Details: “;
car1.display();
cout << “Car 2 Details: “;
car2.display();
cout << “Car 3 Details: “;
car3.display();

return 0;
}

output:

Creating car1 (Using Constructor 1):
Base Class Constructor 1: Vehicle Type Initialized
Derived Class Constructor 1: Car Brand & Year Initialized

—————————–
Creating car2 (Using Constructor 2):
Base Class Constructor 2: Vehicle Type & Wheels Count Initialized
Derived Class Constructor 2: Car Brand, Year, Wheels Initialized

—————————–
Creating car3 (Using Constructor 3):
Base Class Constructor 3: Vehicle Type, Wheels & Fuel Type Initialized
Derived Class Constructor 3: Car Brand, Year, Wheels & Fuel Type Initialized

Car 1 Details: Type: Sedan, Brand: Toyota, Model Year: 2022
Car 2 Details: Type: SUV, Brand: Ford, Model Year: 2021
Car 3 Details: Type: Truck, Brand: Volvo, Model Year: 2020

Benefits of Inheriting a Constructor in C++

When a derived class inherits a base class constructor, it allows automatic constructor reuse. Instead of redefining constructors in the derived class, we can directly use the base class constructors.