Operator Overloading In C++

Operator Overloading In C++

·

5 min read

Abstract : In C++, we can have more than one definition of an operator. In other words, the same operator can be used for multiple data types. Operator overloading is used to redefine an operator.

Scope :

  • This article defines what an operator is and how operator overloading works in C++.
  • This article doesn’t contain the implementation of operator overloading.

Contents :

  • Definition
  • Can we overload every operator in C++?
  • Types of Operator Overloading
  • Is there any operator already Overloaded in C++?
  • Things to Remember while Overloading Operators in C++
  • Summary

Definition : Before moving to Operator Overloading, let’s first understand what an Operator is: Operators are the foundation blocks of any programming language. What if I tell you that we all are using some operators from our Class 1st. You heard it right! We all have been doing the basic math operations such as “Addition”, “Subtraction”, “Division” etc in our schools. The symbols which represent these operations are known as “Operators”.

An Operator is just a symbol that tells the compiler that we are going to perform a specific operation or manipulation. The manipulation here can be mathematical or logical. Like take this “+”, Add symbol, whenever the compiler detects this symbol between any two numbers, it sums up those two numbers. Or we can take this “/”, Division symbol, it tells the compiler that we want to have a division operation here.

Suppose we are adding two numbers “A” and “B”, we will write them as “A+B”, here “A” and “B” are the operands and “+” is the operator.

Since you are now familiar with operators, let’s move to Operator Overloading. Let’s first have the technical definition of what operator overloading is: Operator overloading is a polymorphism in which an operator is redefined to extend or change its use case for a data type. Here data type can be predefined or It can be the user-defined data type (Class).

Now let’s understand this definition : In C++, We can use the “+” addition operator to add two numbers. These numbers can be integers(-2,-1,0,1,2 ...etc.) or can be decimal numbers (-2.5,-1.79,1.09 ...etc.) but, can we add two characters(a,b,c,!,@,#... etc.) using this?

The answer is Yes, we can add characters “a” and “b” to give “ab” as our result. This is possible through operator overloading. We can redefine the “+” operator so that it works on characters too.

Operator overloading is achieved by creating functions. Basic syntax of such functions is given below. ReturnType operator”OperatorSymbol”(parameter(s)){ // Code } For example : Overloading “+” operator for class “Abc” Abc operator+(){ //Code }

In this section, Symbols that represent an operation are known as operators. Modifying use cases of an operator so that it works on the predefined or user-defined data type is operator overloading and it is done by creating functions.

Can we overload every operator in C++? No, there are some operators which can not be overloaded and these are: “::” Scope Resolution Operator “sizeof” “.” Member Selector Operator “*” Member Pointer Selector Operator “?:” Ternary Operator

We can not overload them as they are too close to the core of C++ language and modifying them could lead to programming troubles.

Types of Operator Overloading : Operators can be overloaded in two ways, either by member functions or by friend functions. Let’s discuss them in detail now. Member functions are operators and functions that are defined to be members of a specific class. They exclude operators and functions that have been declared with the “friend” keyword. If we write an operator function as a member function, it gains access to all the member variables of that class. Here is an example of what happens when we overload an operator as a member function. Let “a1” and “a2” be two objects of class “Abc”. Class “Abc” contains the overloading function of the “+” operator. Now, if our expression is a1+a2, the compiler treats it as a1.operator+(a2). This means our overload function gets invoked for the first or left operand here “a1”.

While overloading an operator as a member function we should take care of the following points: The overloaded operator must be a member function of the left operand. Let us suppose we are overloading the “+” operator. It will operate on objects of class “Abc”, hence the overloading function must be present in class “Abc”. From the above example, it is clear that the left operand becomes the implicit “*this” type. Also as we can see other operands here “a2” becomes a parameter.

A non-member function doesn’t have access to private data of the class hence the overloading function must be made a “friend” function of the class. This gives the function access to the private data of the class. While overloading an operator as a friend function we should take care of the following points: When an operator overloading function is declared friend, it takes two operands of user-defined data type. We cannot change an operator's core meaning while redefining its meaning through the operator overloading friend function. For example, we cannot overload the “-” operator to perform division operations.

Is there any operator already Overloaded in C++? Yes, “=” and “&” are overloaded by default in C++. For example “=” operator is used to copy objects of the same class.

Things to Remember while Overloading Operators in C++ : At least one of the operands must be a user-defined class object for operator overloading to operate. Operator overloading does not affect operator precedence or associativity. Existing operators can only be overloaded, whereas new operators cannot. Some operators cannot be overloaded with the help of a friend function. Such operators can be overloaded by using the member function.

Summary:

  • Operators are the symbol that represents an operation in C++
  • Redefining these operators, so that they work on user-defined and predefined data types is known as operator overloading.
  • Operator overloading is achieved by creating functions. This can be done in two ways: member functions or by friend functions.