Function and Operator Overloading in C++ | Become an Expert in 7 Min

As the name itself suggests, the term “overloading” refers to assigning multiple operations to a single entity. So, overloading in C++ refers to the capability of an identifier to be used in order to define multiple methods that differ in terms of parameters.

Today, we’ll explore every aspect of Function and Operator Overloading in C++. This article would help you gain the essence of polymorphism by learning in detail two of its implementation techniques called function overloading and operator overloading.

In order to avoid any confusion between function overloading and polymorphism in C++, refer to this link in which we have made clear the differences between the two.

Keeping you updated with latest technology trends, Join DataFlair on Telegram

Significance of Overloading in C++

It is a healthy practice to use function overloading and operator overloading in C++ because if any errors are present in the code, they are resolved before runtime as function and operator overloading in C++ are types of compile-time polymorphism.

If any errors persist during compile-time, they can easily be rectified and hence any scope of runtime errors are eliminated and the runtime performance of the program increases.

Function and Operator Overloading in C++

What is Function Overloading in C++

C++ offers the use of the concept of function overloading to make the code less redundant by assigning a similar name to different entities by giving various sets of parameters or arguments.

In simple words, two or more functions have the same name but are used for different purposes.

Consider a situation where you want to perform a function display() to display the value of the “2012” using the same name for the function ‘display’ and print it in the form of an integer value and a string.

C++ allows us to overload operators in 3 ways. They are:

  • Methods
  • Constructors
  • Indexed Properties

Enhance your fundamentals skills with Functions in C/C++

C++ Function Overloading Example

Let us better understand how we can implement function overloading in C++ with the help of a program with reference to the above-stated problem:

#include <iostream> 
using namespace std; 

void display(int integer) 
{ 
cout<<"The value in integer form is: "<<integer<<endl; 
} 
void display(string word) 
{ 
cout<<"The value in string form is: "<<word<<endl; 
} 

int main() 
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

display(2012); 
display("Two thousand twelve"); 
return 0; 
}

Code-

Function Overloading in C++ with Example

Output-

Function Overloading in C++ with results

What is Operator Overloading in C++?

Just like function overloading, operator overloading in C++ follows the similar concept of using the same name for different methods.

Operator overloading is achieved with the help of Classes and Objects in C++. C++ gives you the provision to re-define inbuilt operators. In simple words, a C++ programmer can use operators with user-defined types. The keyword ‘operator’ is of immense importance when dealing with operator overloading.

This is how you would declare your own operator:

class_name operator operator_symbol (const class_name&);

We can define most of the overloaded operators as ordinary non-member functions or as class member functions in C++.

In order to define an operator as a non-member function of class, we need to pass two parameters for each operand:

class_name operator operator_symbol (const class_name&, const class_name&);

Refer Operators in C/C++ for more clear understanding 

There are certain operators that the programmer does not have permission to overload. They are as follows:

  • The scope operator “::”
  • The sizeof operator “sizeof(data_type)”
  • The member selector operator “.”
  • The member pointer selector operator “*”
  • The ternary (conditional) operator “?:”

C++ allows the overloading of only certain operations. Here is a list of operators that can be overloaded:

+&<<<+=|=new
|<b>=&=,*=new []
*><=>=++<<=delete
/>><b>==!=&&->delete []
%-=||%=^=>>=()
/=~!->*[]^

Implementation

C++ lets us implement operator overloading in three ways:

  • Member function: If the left operand of that particular class is an object of the same class, then the overloaded operated is said to be implemented by a member function.
  • Non-member function: If the left operand of that particular class is an object of a different class, then the overloaded operator is said to be implemented by a non-member function
  • Friend function: When we need to access the private or protected members of a class, then the operator can be overloaded with the help of a friend function in C++.

Its basic syntax is:

friend return_type operator operator_symbol ( variable1, variable2 )
{
// Statement(s)
}

Rules for Operator Overloading in C++

In addition to that, there are a couple of more boundaries while overloading an operator:

  • We cannot change the precedence and associativity of the operator.
  • We cannot change the number of operands. It means that unary and binary operators would be the same.
  • C++ does not allow the creation of new operators.
  • We do have the permission of redefining the procedure of an operator.

C++ Operator Overloading Example

One of the most popular programs that can be implemented using operator overloading in C++ is the addition or subtraction of two complex numbers.

Let us consider a C++ program to better understand how operator overloading by adding two complex numbers with each other:

#include<iostream> 
using namespace std; 

class Complex 
{ 
private: 
int real, imaginary; 
public: 
Complex(int r = 0, int i =0) // Use of a constructor to initialize and store the values of both the variables
{
real = r; 
imaginary = i;
}

Complex operator + (Complex const &c) 
{ 
Complex result; 
result.real = real + c.real; 
result.imaginary = imaginary + c.imaginary; 
return result; 
}

void display() 
{ 
cout << real << " + " << imaginary <<" i"<<endl; 
} 
}; 

int main() 
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

Complex c1(3, 5), c2(2, 8); 
Complex c3 = c1 + c2; 
c3.display(); 
return 0;
}

Code-

Example of C++ Operator Overloading

Output-

Output of C++ Operator Overloading Example

Summary

Overloading is the widely used concept of OOP. You can find many questions related to operator and functional overloading in interviews and Quizzes. If you are master in C++ overloading, you can easily land your dream job. Now, it’s your turn to practice and showcase your talent. Hope, it will help you in taking a step forward towards your success.

It’s time to move on the next article – Exception Handling in C++ 

Enjoyed this article?? Share your experience in the comment section!

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.