C++ Sample Code to Display Operator Overloading

Operator Overloading is a special name given to the process of defining custom functions for operators. In simple terms in order to overload an operator like -, +, =, ++, –, etc you simply create a function for it. This Sample C++ Project displays how easy and quick is to implement and use Operator Overloading in your C++ Classes.

Operator Overloading Example Source Code in C++

Operator Overloading Example Source Code in C++

In this sample C++ Project a simple class has been created with a private member variable, a public constructor, a public function and another function which is overloading the work of the ++ operator. In case you understand the last line, you are ready to delve into operator overloading and within no time you can overload almost any operator. Remember that member functions of a C++ class have return data type, a function name and optionally input parameters. When you are overloading an operator, the function name is a combination of the operator keyword and the operator you wish to overload. That’s it now have a close look at the screenshot above and note that the special function name operator ++ .

Using Object with ++ Operator Overloaded

Using Object with ++ Operator Overloaded

The above screenshot displays the main function of this sample C++ project using the operator overloaded class. Given below output of this sample C++ project can be used to re-confirm your understanding of operator overloading. Full Source Code of this Sample C++ Application is provided at the end of this post which can be used in your project to try operator overloading on your own Computer.

Console Output of Sample Project for Operator Overloading

Console Output of Sample Project for Operator Overloading

In case of operator overloading, only the member function name is different and rest of all are the same. As described above, the function name for overloading the operator ++ became operator ++ in this sample C++ project. Feel free to use the sample source code provided given below to try operator overloading with other types of operators.

// OperatorOverloadingForClass.cpp : Defines the entry point for the console application.// http://www.Tapkaa.com/ for More C++ Samples and Projects#include "stdafx.h"#include <iostream>using namespace std;class COperatorOverloadingForClass{int m_iSimpleCounter;public:COperatorOverloadingForClass(){m_iSimpleCounter=0;}//Public Member Function Displaying Value of a Private Member Variablevoid DisplayCurrentValueOfCounter(){cout << "Current Value of Simple Counter is " << m_iSimpleCounter << "n";}//This is an Example of Operator Overloading//This Function will increment the value of member variablevoid operator ++ (){m_iSimpleCounter=m_iSimpleCounter+1;}};int _tmain(int argc, _TCHAR* argv[]){COperatorOverloadingForClassvl_oObjectForOperatorOverloading;vl_oObjectForOperatorOverloading.DisplayCurrentValueOfCounter();++vl_oObjectForOperatorOverloading;vl_oObjectForOperatorOverloading.DisplayCurrentValueOfCounter();return 0;}