Overloading in C++ (Function & Operator)

Overloading in C++ (Function & Operator)

C++ allows you to create more than one methods having the same name in the same scope. Their number of input parameters or its type differ. So if we create 2 or more methods having the same names and different parameters, it is called as overloading. Types of overloading in C++ are:
1. Function overloading
2. Operator overloading

When you call an overloaded function or operator, the compiler determines the most appropriate definition to use. It compares the argument types used to call the function or operator with the parameter types specified in their definitions. We can overload methods, constructors, and indexed properties.

1. Function overloading

Two functions having the same name but different arguments/parameters is called as Function overloading. It’s advantage is that it increases the readability of a program. You don’t need to use different names for the same action. So let’s see the simple program of function overloading:

#include <iostream>
using namespace std;
int volume(int a)
{
  return a*a*a;
}
int volume(int a, int b, int c)
{
  return a * b * c;
}

int main()
{
  cout << " Volume of cube = " << volume(10);
  cout << "\n Volume of cuboid = " << volume(10, 6, 8);
  return 0;
}

Output-

overloading

Explanation-

In this program, we overloaded the volume() function. There are 2 functions named volume but the first one has single parameter and the other has 3 parameters. The types of parameters decide the function to be called. Thus it calculates and displays output for each.

2. Operator overloading

Redefining built-in operators available in C++ is called as Operator overloading. Overloaded operators are functions with special names. The name of an operator function is always the keyword operator followed by the symbol of the operator. e.g operator+, operator-. All the operators except . (dot), :: (scope resolution), ?: (ternary), sizeof can be overloaded. So let’s see the simple program of operator overloading:

#include <iostream>
using namespace std;
class Height {
  public:
    int feet, inch;
  Height(int f, int i) {
    feet = f;
    inch = i;
    cout << "Feet & Inches before decrement= " << feet << "'" << inch << endl;
  }
  void operator- () {
    feet--;
    inch--;
    cout << "Feet & Inches after decrement= " << feet << "'" << inch;
  }
};
int main() {
  Height h(6, 2);
  -h;
  return 0;
}

Output-

Explanation-

In this program, we overload ‘–’ unary operator to work as ‘–‘ decrement. We pass two parameters to the constructor and save their values in feet and inch variable. Then the operator- function decrements the variable by one. -h calls the operator overloading function and decrements the values passed to the constructor. Finally new values are displayed in the output.

3. Parts of program-

1. #include is pre-processor directive used to include header files.
2. iostream is header file which has certain commands that C++ supports. E.g. return, main, etc.
3. int is a data type.
4. main() is main function that indicates the compiler that the user written programs starts from here.
5. cout is the keyword use to print a message.
6. return statement is use to return a value. If any error occurs the program will return 0.

Conclusion-

So this was the concept of Overloading in C++. We hope it was clear to you. If you find it useful then do share it with your programming buddies and friends. Also if you have any doubt regarding any post do tell us in the comment section.

For daily updates do follow us on Instagram. Visit again!

ErrorFreeProgram.

2 COMMENTS

comments user
https://www.shinsen-mart.com

Best view i have ever seen !

Leave a Reply

Your email address will not be published. Required fields are marked *