C++ Method Overloading

C++C++Beginner
Practice Now

Introduction

In this lab, we will learn how to demonstrate the concept of Method Overloading in the C++ programming language. Method Overloading is a concept where we can have multiple methods or functions with the same name in a class, but with different parameters. The appropriate method is called based on the number and types of parameters passed during the method call.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp(("`C++`")) -.-> cpp/OOPGroup(["`OOP`"]) cpp/SyntaxandStyleGroup -.-> cpp/comments("`Comments`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") cpp/OOPGroup -.-> cpp/classes_objects("`Classes/Objects`") cpp/OOPGroup -.-> cpp/class_methods("`Class Methods`") cpp/OOPGroup -.-> cpp/access_specifiers("`Access Specifiers`") subgraph Lab Skills cpp/comments -.-> lab-96149{{"`C++ Method Overloading`"}} cpp/variables -.-> lab-96149{{"`C++ Method Overloading`"}} cpp/data_types -.-> lab-96149{{"`C++ Method Overloading`"}} cpp/operators -.-> lab-96149{{"`C++ Method Overloading`"}} cpp/function_parameters -.-> lab-96149{{"`C++ Method Overloading`"}} cpp/classes_objects -.-> lab-96149{{"`C++ Method Overloading`"}} cpp/class_methods -.-> lab-96149{{"`C++ Method Overloading`"}} cpp/access_specifiers -.-> lab-96149{{"`C++ Method Overloading`"}} end

Create a C++ file

First, we create a new C++ file named main.cpp in the ~/project directory using the following command:

touch ~/project/main.cpp

Write the code

We will create a class named shape and define two methods named area(), but with different numbers of parameters. The input() method will take input from the user to set the values of member variables l, b, and s. We will then define main() method to access the members of the shape class from outside the class.

#include <iostream>

using namespace std;

class shape {

    //declaring member variables
    public:
        int l, b, s;

    //defining member functions or methods
    public:
        void input() {
            cout << "Enter the length of each side of the Square: \n";
            cin >> s;
            cout << "\n";
            cout << "Enter the length and breadth of the Rectangle: \n";
            cin >> l >> b;
            cout << "\n";
        }

    //Demonstrating Method Overloading
    public:
        void area(int side) {
            cout << "Area of Square = " << side * side << endl;
        }

        void area(int length, int breadth) {
            cout << "Area of Rectangle = " << length * breadth << endl;
        }
};

int main() {
    cout << "\n\nWelcome to LabEx :-)\n\n\n";
    cout << " =====  Program to demonstrate Method Overloading in a Class, in CPP  ===== \n\n";

    //Declaring class object to access class members from outside the class
    shape sh;

    cout << "\nCalling the input() function to take the values from the user\n";
    sh.input();

    cout << "\nCalling the area(int) function to calculate the area of the Square\n";
    sh.area(sh.s);

    cout << "\nCalling the area(int,int) function to calculate the area of the Rectangle\n";
    sh.area(sh.l, sh.b);

    cout << "\nExiting the main() method\n\n\n";

    return 0;
}

Compile and run the code

We will now compile and run the code using the following command:

g++ main.cpp -o main && ./main

Understand the output

The output of the program will be as follows:

Welcome to LabEx :-)


 =====  Program to demonstrate Method Overloading in a Class, in CPP  =====

Calling the input() function to take the values from the user
Enter the length of each side of the Square:
4

Enter the length and breadth of the Rectangle:
5
6


Calling the area(int) function to calculate the area of the Square
Area of Square = 16

Calling the area(int,int) function to calculate the area of the Rectangle
Area of Rectangle = 30

Exiting the main() method

Here, we can see that the input() method is called to accept user input. The area() method with one parameter is then called to calculate the area of a square, and the area() method with two parameters is called to calculate the area of a rectangle.

Summary

In this lab, we learned how to use the method overloading concept in C++ programming language. We created a program that defines a class named shape with two methods named area(), but with different numbers of parameters, which have been overloaded. The user inputs the length and breadth of a rectangle and length of a side of a square. We used the overloaded methods to calculate the areas of both shapes.

Other C++ Tutorials you may like