Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Operator overloading in C++ OOP , Basic program of Operator overloading in OOP C++

Operator overloading in C++ OOP , Basic program of Operator overloading in OOP C++

In this tutorial, we will learn about the followings;

  1. Operator overloading in C++ OOP 
  2. The basic program of Operator overloading in OOP C++

What is Operator overloading?

Operator overloading is an example of polymorphism in which operators like-, +, == etc are working as polymorphic functions This different behavior depends on the types of its arguments.

Basic program of Operator overloading

#include <iostream>
using namespace std;

class example
{
int a,b;
public:
void input()
{
cout<<“Enter a and b: “;
cin>>a>>b;
}
void operator -()
{
a=-a;
b=-b;
}
void display()
{
cout<<“a=”<<a<<endl<<“b=”<<b<<endl;
}
};

int main()
{
example e;
e.input();
cout<<“Before overloading the operator”<<endl;
e.display();
-e;
cout<<“After overloading the operator”<<endl;
e.display();
return 0;
}