C++ Overloading (Operator and Function)3 min read

C++ allows us to overload methods, constructor and indexed properties. The function or operator define with the same name but with different types of parameters s called overloading. C++ has two types of overloading and they are:

  • Function overloading
  • Operator overloading

C++ Function overloading

Function overloading is a feature of object-oriented programming where multiple functions having the same name differs by their parameters, then these functions are said to be overloaded. When we call those functions, we need to make sure to enter the proper number of arguments along with the proper data type.

Example: Below are the sample of functions with same name but different arguments.

Error example: Now see the example below, the functions defined below have the same name, same number type, and the same number of arguments. In such cases, the compiler will through an error.

The advantage of function overloading is that we do not need to write different names for the functions that perform the same task, increasing the readability of the program. Polymorphism is achieved by function overloading.

Let us go through an example in C++ for function overloading.

C++ program for function overloading with different types and different number of parameters

Output:


C++ Operator Overloading

Operator overloading means performing different operations with the same operator. We can overload most of the built-in operators that are available in C++. We can also change the user-defined type as well such as objects and structures.

The keyword ‘operator‘ is used to overload an operator. Compile-time polymorphism is achieved through operator overloading. It has a return type and parameter list.

Example: Consider an operator ‘+‘, it is used to add the two numbers but it is also used with string to concatenate two strings. So the ‘+‘ operator when placed between number operands, addition operation is done but when placed between string operands, it concatenates them.

Syntax: syntax for operator overloading in C++.

In the above syntax,

  • return_type is the type that is returned by the function
  • symbol refers to the operator that is to be overloaded such as +, <, -, >, etc.

However there are few operator that cannot be overloaded and they are:

  • sizeof operator
  • Scope operator (::)
  • ternary operator(?:)
  • member pointer selector(*)

Let us go through an example in C++ for operator overloading.

C++ Program for operator overloading

Output:


Another example for operator overloading in C++.

Output:

The advantage of operator overloading is that we can perform different operations on the same operand.


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …