Home>

The so-called overload,Is to give new meaning.Function overloading is to give new meaning to an existing function.To enable new features,Therefore, a function name can be used to represent functions with different functions.That is "a multi-purpose".

Operators can also be overloaded.In fact, we have used operator overloading unknowingly.For example, everyone is used to adding integers, single precision numbers, and double precision numbers using the addition operator "+",Such as 5 + 8, 5.8 + 3.67, etc. In fact, the computer's process of adding integers, single precision numbers and double precision numbers is very different. But because C++ has overloaded the operator "+",So it can be applied to int, float, double type operations.

Another example is "<<" is the shift operator (left shift) in the bit operation of C++, but in the output operation is the stream insertion operator used with the stream object cout">" is also a shift operator (shift right), but is also a stream extraction operator used with the stream object cin in input operations.This is operator overloading. The c++ system overloads "<">"When users use them in different situations,The effect is different. The overloading of "<" and ">" is placed in the header file stream. Therefore, if i want to use "<" and ">" as a stream insertion operator and a stream extraction operator in your program,The header file stream must be included in this file module (of course it should also include "using namespace std").

The question to be discussed now is:Can users overload the operators provided by C++ according to their needs,Give them new meaning,Make one name more versatile.For example, is it possible to add two complex numbers with a "+" sign.In C++, the operator "+" cannot be used to add complex numbers directly in a program.The user must try to implement the complex addition by himself.For example, users can add complex numbers by defining a special function.

[Example] Adding complex numbers through functions.

#include<iostream>
using namespace std;
class complex //Define the complex class
{
public:
  complex () {real=0;imag=0;} //define the constructor
  complex (double r, double i) {real=r;imag=i;} //Constructor overload
  complex complex_add (complex & c2);//Declares complex addition function
  void display ();//declare output function
private:
  double real;//real part
  double imag;//imaginary part
};
complex complex ::complex_add (complex&c2)
{
  complex c;
  c.real=real + c2.real;
  c.imag=imag + c2.imag;
  return c;
}
void complex ::display () //Define output function
{
  cout<""<<real<<<<<><<<>
}
int main ()
{
  complex c1 (3,4), c2 (5, -10), c3;//Define 3 complex number objects
  c3=c1.complex_add (c2);//Call complex number addition function
  cout<<"c1 =";c1.display ();//output the value of c1
  cout<<"c2=";c2.display ();//output the value of c2
  cout<<"c1 + c2=";c3.display ();//output the value of c3
  return 0;
}

The results are as follows:

c1=(3 + 4i)
c2=(5-10i)
c1 + c2=(8, -6i)

The result is undoubtedly correct,But the calling method is not intuitive. It is too cumbersome and inconvenient.Can it be the same as the addition of integers,What about using the plus sign "+" for complex numbers?Such as

 c3=c1 + c2;

The compilation system will automatically complete the addition of two complex numbers c1 and c2.If it can be done,Provides great convenience for the calculation of objects.This requires overloading the operator "+".

c
  • Previous Rsync achieve file timing synchronization skills
  • Next Remote shutdown and restart using shutdown command