Overloading Binary Operator
Write a C++ program to overload binary operator '+' to add two complex numbers.
Output:
Why do we need Operator overloading in C++?
Operators in C++ like
Such a way of extending the operational functionality of certain operators in C++ is called operator overloading.
+, -, *, / can operate in datatypes like int, float, double etc as predefined operational meanings. But these operators can’t operate in user-defined datatypes like objects without extension or adding some sort of code to alter their operational meaning.Such a way of extending the operational functionality of certain operators in C++ is called operator overloading.
Which operators can we overload and which we cannot?
Following are the operators that cannot be overloaded
- pointer to member access operator ( .* )
- scope resolution operator ( :: )
- member access operator ( . )
- condition operator ( ?: )
- size operator ( sizeof )
- run-time type information operator ( typeid )
Following is the list of overloadable operators
| Category | Operators |
| Airthmetic | + , – , * , / , % |
| Bit-wise | & , | , ~ , ^ , << , >> |
| Bit-wise assignment | &= , |= , ^= , <<== , >>= |
| Relational | < , > , == , != , <= , >= |
| Logical | || , && , ! |
| Assignment | = |
| Arithmetic assignment | -=, += , *= , /= , %= |
| Unary | ++ , — |
| Subscripting | [ ] |
| Deference | * |
| Function call | ( ) |
| Address of | & |
| Member access through member pointer | ->* |
| Member access through object pointer | -> |
| Dynamic Allocation and release | new, delete, new[ ], delete[ ] |
| Comma | , |
#include<iostream>
using namespace std;
class Complex
{
int num1, num2;
public:
void accept()
{
cout<<"\n Enter Two Complex Numbers : ";
cin>>num1>>num2;
}
Complex operator+(Complex obj) //Overloading '+' operator
{
Complex c;
c.num1=num1+obj.num1;
c.num2=num2+obj.num2;
return(c);
}
void display()
{
cout<<num1<<"+"<<num2<<"i"<<"\n";
}
};
int main()
{
Complex c1, c2, sum; //Created Object of Class Complex i.e c1 and c2
c1.accept(); //Accepting the values
c2.accept();
sum = c1+c2; //Addition of object
cout<<"\n Entered Values : \n";
cout<<"\t";
c1.display(); //Displaying user input values
cout<<"\t";
c2.display();
cout<<"\n Addition of Real and Imaginary Numbers : \n";
cout<<"\t";
sum.display(); //Displaying the addition of real and imaginary numbers
return 0;
}
using namespace std;
class Complex
{
int num1, num2;
public:
void accept()
{
cout<<"\n Enter Two Complex Numbers : ";
cin>>num1>>num2;
}
Complex operator+(Complex obj) //Overloading '+' operator
{
Complex c;
c.num1=num1+obj.num1;
c.num2=num2+obj.num2;
return(c);
}
void display()
{
cout<<num1<<"+"<<num2<<"i"<<"\n";
}
};
int main()
{
Complex c1, c2, sum; //Created Object of Class Complex i.e c1 and c2
c1.accept(); //Accepting the values
c2.accept();
sum = c1+c2; //Addition of object
cout<<"\n Entered Values : \n";
cout<<"\t";
c1.display(); //Displaying user input values
cout<<"\t";
c2.display();
cout<<"\n Addition of Real and Imaginary Numbers : \n";
cout<<"\t";
sum.display(); //Displaying the addition of real and imaginary numbers
return 0;
}
Output:

Comments
Post a Comment