I found the below piece of code on the internet.I need help regarding binary operator overloading.In the main funtion,,result=c1-c2;<-this statement has been written.I just wanted to know,,does this statement mean that the object c1 is invoking the overloaded operator function,,and the other object is being sent as the argument,and the result is being stored in the object "result"?.Its a bit confusing.So like we use the dot operator most of the times,to access a member function,the same way,we are using the - operator in this case to access the overloaded operator function?Thanx a ton to all who post their replies!God bless you all!! :D:D
#include <iostream>
usingnamespace std;
class Complex
{
private:
float real;
float imag;
public:
Complex(): real(0), imag(0){ }
void input()
{
cout<<"Enter real and imaginary parts respectively: ";
cin>>real;
cin>>imag;
}
Complex operator - (Complex c2) /* Operator Function */
{
Complex temp;
temp.real=real-c2.real;
temp.imag=imag-c2.imag;
return temp;
}
void output()
{
if(imag<0)
cout<<"Output Complex number: "<<real<<imag<<"i";
else
cout<<"Output Complex number: "<<real<<"+"<<imag<<"i";
}
};
int main()
{
Complex c1, c2, result;
cout<<"Enter first complex number:\n";
c1.input();
cout<<"Enter second complex number:\n";
c2.input();
/* In case of operator overloading of binary operators in C++ programming, the object on right hand side of operator is always assumed as argument by compiler. */
result=c1-c2; /* c2 is furnised as an argument to the operator function. */
result.output();
return 0;
}
.I just wanted to know,,does this statement mean that the object c1 is invoking the overloaded operator function,,and the other object is being sent as the argument,and the result is being stored in the object "result"?.