C++ Constructor Overloading

Constructor Overloading

Constructor overloading in C++ is a concept where a class can have more than one constructor, each with a different number of parameters or types of parameters. This allows objects of the class to be initialized in multiple ways. Constructor overloading is a key feature of object-oriented programming that adds flexibility to how objects can be created.

Example of Constructor Overloading: ‘Pizza’ Ordering System

Imagine you’re at a pizza shop where you can order a pizza in various ways:

  1. Basic Pizza: Just the base with no toppings.
  2. Pizza with Toppings: Specify one or more toppings.
  3. Customized Pizza: Specify the base, toppings, and size.

We’ll create a Pizza class in C++ to demonstrate constructor overloading, allowing different ways to order a pizza.

Code

#include <iostream>
#include <string>
using namespace std;

class Pizza {
public:
    string base;
    string toppings;
    string size;

    // Default Constructor: Basic Pizza
    Pizza() {
        base = "Thin Crust";
        toppings = "No toppings";
        size = "Medium";
        cout << "Basic Pizza: " << base << ", " << toppings << ", " << size << endl;
    }

    // Parameterized Constructor 1: Pizza with Toppings
    Pizza(string t) {
        base = "Thin Crust";
        toppings = t;
        size = "Medium";
        cout << "Pizza with Toppings: " << base << ", " << toppings << ", " << size << endl;
    }

    // Parameterized Constructor 2: Fully Customized Pizza
    Pizza(string b, string t, string s) {
        base = b;
        toppings = t;
        size = s;
        cout << "Customized Pizza: " << base << ", " << toppings << ", " << size << endl;
    }
};

int main() {
    // Ordering a basic pizza
    Pizza pizza1;

    // Ordering a pizza with specific toppings
    Pizza pizza2("Pepperoni");

    // Ordering a fully customized pizza
    Pizza pizza3("Cheese Burst", "Mushrooms, Olives", "Large");

    return 0;
}

Output:

Basic Pizza: Thin Crust, No toppings, Medium
Pizza with Toppings: Thin Crust, Pepperoni, Medium
Customized Pizza: Cheese Burst, Mushrooms, Olives, Large

Explanation:

Step 1: Default Constructor : Basic Pizza

Pizza() {
    base = "Thin Crust";
    toppings = "No toppings";
    size = "Medium";
    cout << "Basic Pizza: " << base << ", " << toppings << ", " << size << endl;
}

Purpose:

  • This constructor initializes a Pizza object with default values: a “Thin Crust” base, “No toppings,” and “Medium” size.

Example Usage:

  • When you create an object using this constructor, like Pizza pizza1;, it will produce a basic pizza without any customization.

Output:

  • When pizza1 is created, it outputs: "Basic Pizza: Thin Crust, No toppings, Medium"

Step 2 : Parameterized Constructor 1: Pizza with Toppings

Pizza(string t) {
    base = "Thin Crust";
    toppings = t;
    size = "Medium";
    cout << "Pizza with Toppings: " << base << ", " << toppings << ", " << size << endl;
}

Purpose:

  • This constructor allows you to specify the toppings while keeping the base and size as defaults. For example, you can create a pizza with “Pepperoni” as the topping.

Example Usage:

  • When you create an object using this constructor, like Pizza pizza2("Pepperoni");, it will produce a pizza with the specified topping.

Output:

  • When pizza2 is created, it outputs: "Pizza with Toppings: Thin Crust, Pepperoni, Medium"

Step 3: Parameterized Constructor 2: Fully Customized Pizza

Pizza(string b, string t, string s) {
    base = b;
    toppings = t;
    size = s;
    cout << "Customized Pizza: " << base << ", " << toppings << ", " << size << endl;
}
  • Purpose:
    • This constructor allows full customization of the pizza. You can specify the base, toppings, and size. For example, you can create a pizza with a “Cheese Burst” base, “Mushrooms, Olives” toppings, and “Large” size.
  • Example Usage:
    • When you create an object using this constructor, like Pizza pizza3("Cheese Burst", "Mushrooms, Olives", "Large");, it will produce a fully customized pizza.
  • Output:
    • When pizza3 is created, it outputs: "Customized Pizza: Cheese Burst, Mushrooms, Olives, Large"

Step 4: Main Function: Using Overloaded Constructors

int main() {
    // Ordering a basic pizza
    Pizza pizza1;

    // Ordering a pizza with specific toppings
    Pizza pizza2("Pepperoni");

    // Ordering a fully customized pizza
    Pizza pizza3("Cheese Burst", "Mushrooms, Olives", "Large");

    return 0;
}

Explanation:

  • pizza1:
    • Created using the default constructor, resulting in a basic pizza with a “Thin Crust,” “No toppings,” and “Medium” size.
  • pizza2:
    • Created using the parameterized constructor with one argument, specifying the topping “Pepperoni,” while the base remains “Thin Crust” and the size “Medium.”
  • pizza3:
    • Created using the fully parameterized constructor, customizing the pizza with a “Cheese Burst” base, “Mushrooms, Olives” toppings, and “Large” size.

Summary

  • Constructor Overloading:
    • The Pizza class demonstrates constructor overloading by providing different ways to create a pizza object, whether it’s a basic pizza, a pizza with specific toppings, or a fully customized pizza.
  • Real-Life Analogy:
    • The different constructors simulate the experience of ordering a pizza at a shop where you can choose a basic pizza, add some toppings, or fully customize your pizza with different base, toppings, and size.

    Leave a Reply

    Your email address will not be published.

    Need Help?