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;
}

Please Share This Article with Friends
Fazal Rehman Shamil
Welcome to all friends. The reason for our success is only your love for T4Tutorials. Our team is always available to answer your queries regarding any kind of confusions or discussion regarding your study and career matters. For discussion with us please join our facebook group "T4Tutorials.com". The link of the group is mentioned below. Thanks and love to all for connecting with us. We are nothing without you. Love you all.....
https://web.facebook.com/groups/2066136233601097/