Method details for C++ operator overloading

  • 2020-04-02 01:50:41
  • OfStack

Operator overloading is essentially an overloading of a function

The general format of overloaded operator functions is as follows:

Function types       operator   Operator name       (form parameter list column)

{overload handling of operators}

For example, if you want to use "+" for the addition operation of Complex Numbers, the prototype of a function can look like this:


Complex operator + (Complex & c1,Complex &c2);

Where the operator is the keyword that is used to define the function of the overloaded operator, the operator name is the predetermined operator provided by C++ to the user.

Note: the function name is composed of an operator and an operator.

< img Alt = "" border = 0 SRC =" / / img.jbzj.com/file_images/article/201310/201310150853413.jpg ">

The above operator+ is the name of the function, which means "pair operator+ overload". Once you've got that, you can see that this type of function is formally indistinguishable from any other function.

The two parameters are references to Complex class objects, and the arguments are required to be Complex class objects.

After defining the overloaded operator, you can say: the operator+ overloads the operator+.

When the expression c1+c2 for adding Complex Numbers is executed (assuming that c1+c2 is already defined as Complex), the system will call the operator+ function, using c1+c2 as an argument to combine the imaginary real with the formal parameter.

To illustrate that the execution of the expression after the operator is overloaded is the process of calling the function, you can also imagine adding two integers as calling the following function:


int operator + (int a,int b)
{
          return (a+b);
}

If you have an expression 5+8, you call this function, and you take 5 and 8 as parameters when you call the function, and the return value of the function is 13. That's how you understand the operator through the method of the function.

= = = = = = = = = = = = = = = = = = = = = sample code 1.1 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =


#include<iostream>
using namespace std;
class Complex
{
 public:
  Complex()
  {
   real=0;
   imag=0;
  }
  Complex(double r,double i)
  {
   real=r;
   imag=i;
  }
  Complex operator + (Complex &c2);//The "+" function that declares the operator
  void display();
 private:
  double real;
  double imag;
};
Complex Complex::operator+(Complex &c2)
{
 Complex c;
 c.real=real+c2.real;
 c.imag=imag+c2.imag;
 return c;
}
void Complex::display()
{
 cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
 Complex c1(3,4),c2(5,-10),c3;
 c3=c1+c2;
 cout<<"c1=";
 c1.display();
 cout<<"c2=";
 c2.display();
 cout<<"c3=";
 c3.display();
  return 0;
}

< img Alt = "" border = 0 SRC =" / / img.jbzj.com/file_images/article/201310/20130911000012765.png ">

Analysis:

In main, "c3=c1+c2;" After overloading the operator + as a member function of the class, the C++ compiler interprets the expression c1+c2 in the program as:

C1. Operator + (c2); // where c1+c2 is the object of the Complex class

That is to call the operator+(Complex) operator+(Complex) of c1 with c2 as an argument & C2), evaluate it, get the sum of the two complex Numbers. The above "operator+" is a function name that is a member of the class Complex.

In practice, class declarations and class usage are often separated. If the operators +, -, *, / are overloaded when declaring the Complex class, users of this class can program without considering how the function is implemented.

You can easily use +, -, *, / to calculate complex Numbers.

You can also rewrite the operator + function more concisely:


Complex Complex::operator+(Complex &c2)
{
 return Complex(c2.real+real,c2.imag+imag);
}

Complex(c2.real+real,c2.imag+imag) in the return statement is to create a temporary object that has no object name and is an unnamed object.

During the creation of the temporary object, the constructor is called. The return statement returns this temporary object as a function.

So, after we overloaded the + operator, can we add a constant to a complex number? Such as:


c3=3+c2;        //Error. Does not match parameter type

This doesn't work, because when we define the operator + function, the parameter is an object of two Complex, that is, the argument and parameter match to call the function.

Should be written in object form, such as:


c3=Complex(3,0)+c2;     //Correct, all types are objects

It is also important to note that when an operator is overloaded, its original functionality remains intact and is not lost or changed.

For example, when the operator + is overloaded, it can still be used for operations on int, float, double, and char data, while adding the function for adding complex Numbers.

The compilation system is determined by the context of the expression, that is, by the data type on both sides of the operator (or one side if it is a monocular operator).

For example, for 3+5, integer addition is performed; For 3.4+5.45, double addition is performed; Complex addition is performed for the addition of two complex classes.


Related articles: