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

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More