Operator overloading in C++

An operator usually has the same definition when used in variables of basic types like float, int, double, etc. For example, the + operator is used to add two integers. However, we can redefine the way an operator works for user-defined types like objects. In C++, we can redefine the meaning of the + operator and use it to concatenate strings if we have two objects of a class that has a string as its data members.

Operator overloading is a unique feature in C++ that allows programmers to redefine the meaning of an operator. It is also the ability to provide operators with a special meaning for a data type. Arithmetic operators that can also be overloaded include classes like complex number, big integer, fractional number, etc.

Why operator overloading?

You can write a C++ program even if you are not acquainted with operator overloading. However, your program will not be intuitive. For example, a code like:

Calculate = add (multiply) (a, b), divide (a, b);

Can be replaced by

Calculate = (a*b) + (a/b);

How do we overload operators in C++ programming?

We have to define a special operator function inside class to overload an operator. Check out the code below:

classclassName

{

. .. . . . .. . ..

Public

returnType operator symbol (arguments)

{

.. .. . . …..

}

, ,, , , , ,,    ,

};

Where:

  • returnType is the return type of a function
  • Operator symbol is the arithmetic symbol that you want to overload like +, ++, – , <, etc.
  • We pass arguments to operator functions the same way we do with normal functions

Our C++ programming homework helpers have highlighted the following as some of the things about operator overloading that you should always remember

  • Operator overloading cannot be used for built-in data types like char, float, int, etc. it allows programmers to redefine the way operators work for user-defined data types (objects and structures) only
  • In C++, the & and = operators are already overloaded by default. You can directly use the + operator to copy objects of the same class without creating an operator function.
  • The precedence and associativity of operators do not change when we use operator overloading. However, it is recommended that you use parenthesis if you want to change the order of evaluation.
  • In C++, you cannot overload operators like ternary operator (?:), scope resolution (: :), member selection through pointer to a function (.*), and member selection (.).
  • The compiler automatically creates a default assignment operator with every class.
  • Conversion operators can be written and be used to convert one type to another. For example, the program below converts fraction number to float type:

#include <iostream>

Using namespace std;

Class Fraction

{

Intnum, den;

Public

Fraction (int n, int d) { num = n; den = d; }

//The conversion operator will return the float value of the fraction

Operator float () const {

return float (num)/ float (den) ;

}

};

Int main () {

Fraction f ( 2, 4) ;

Float val = f;

Cout<<val ;

return 0;

}

The output generated is:

0.5

Frequently Asked Questions on overloading operators in C++

  • Is there a difference between normal functions and operator functions?

Normal functions are almost the same as operator functions. The main difference between the two is that operator functions are always called when the corresponding operator is used. Also, the naming of an operator function starts with the operator keyword, followed by the symbol of the operator.

  • Can all operators be overloaded?

We can overload all operators apart from the ternary operator (?:), scope resolution (: :),  member selection through pointer to a function (.*), Sizeof, and member selection (.).

  • Why can’t we overload the operators above?

We cannot overload the sizeof operator because built-in operations (like incrementing pointer into an array implicitly) depends on it. We cannot give the operator a new meaning without violating the basic rules of the C++ language. Overloading the dot (member selection operator) raises the question of whether the operation is meant for the object referred to or the object overloading. Finally, there is no fundamental reason why overloading the ternary operator is disallowed. The developers of C++ didn’t see the need to introduce the special case of overloading the ternary operator.

Avail of our help with C++ programming assignment if you are facing hurdles with operators overloading in C++.