Operator overloading in classes in C++

Written by Hamza on. Posted in C++

After a lengthy break, i’m back with my series on object oriented programming in C++ made easy for beginners. As you may recall, I had started this series as a sort of  a basic guide for programmers who were just delving into the basics of OOP in C++. I hope by now all of you understand the basic concepts behind classes and how they are used. All of that prior knowledge will be required, as today I am going to be talking about the concept of operator overloading in classes in C++.

Overloading operators is covered quite a bit on this website already, so I am just going to give you an introduction and touch on it. If you want further details feel free to browse through the archives of codingmash.

So getting on with the topic at hand, overloading operators is a very simple concept in itself. As i’ve explained before, when we define a class, it becomes a sort of data type that we can use, just like we use ‘int’ or ‘char’ or ‘float’. Now, in the cases of int or float, we know what the standard mathematical operators like ‘+’ and ‘/’ should do to any number of integers and float point numbers. But, say we have two objects of the fraction class that we have used so often in this series. In real life, we know how to add two fractions, but simply writing a ‘+’ between two objects of the fraction class wont correctly add the two objects together. This is not even syntactically allowed in C++.

C++ offers an excellent solution to this problem. It gives us the power to overload operators, that is, to redefine what functions the operators perform on our objects. Thus, if overloaded correctly, two objects of the fractions class would, when added together, produce the result of a third object which will correctly add the numerator and denominator data members of the objects like we do in real life. Using an operator on objects in ONLY allowed when we have used operator overloading on that operator.

IMP: Before moving on, here is an important article, dealing with the rules and conventions of operator overloading. Once you’re done with those, move back to this article.

Okay, now we can explain further by laying out this example of the fraction class. Here is the code.

class fraction


. private:

. int numerator;

. int denominator;

. public:

//assume getters and setters are declared

//assume copy constructor is declared

. fraction operator + ( const fraction & obj2 )
. {

. fraction temp;

.  temp.denominator = this->denominator * obj2.denominator;

.  temp.numerator = (this->numerator * obj2.denominator) + (obj2.numerator * this->denominator);

return temp;

. }


int main()


. fraction obj1;

. fraction obj2;

//assume now I have set obj1 and obj2 to '3/4' and '2/4'

. fraction obj3 = obj1 + obj2;


For this to work, a copy constructor needs to be written. I will explain more about this concept later.

The code is pretty easy. The things to note are the syntax of the the operator overloading function, the return type and the parameter passed in the function. As a rule of thumb, the operand on the right of a binary operator is passed as a parameter into the function and the operand on the left is accessed through the this pointer, i.e, within the class. The third object, the object which stores the result of the operation is used as the return type.

This example only deals with the ‘+’ operators. There are many other operators that can be overloaded, and some are done so using different techniques. As I have mentioned, codingmash has extensive detail on this, so hurry up and look at those articles if you want every detail! For now, this knowledge should be enough for beginners to move on in the series.

Tags: , , , ,

Trackback from your site.

Leave a comment