Introduction to operator overloading in C++ programming
- 2020-05-05 11:36:32
- OfStack
So-called overload, is to give a new meaning. Function overloading is to give new meaning to an existing function, so that it can achieve new functions, so that a function name can be used to represent the function of different functions, that is, "one multi-function."
Operators can also be overloaded. In fact, we've used operator overloading without realizing it. Big house, for example, have been accustomed to use the addition operator "+" for integers, single precision number and double addition operation, such as 5 + 8, 5.8 + 3.67, in fact, the computer for integers, single precision number and double addition operation process is not the same, but because C + + has to the overloaded operator "+", so they can apply to the int, float, doUble type of operation.
Such as "< again < "Is the displacement operator (left shift) in the bit operation of C++, but in the output operation is the stream insertion operator used in conjunction with the stream pair cout," > > "Is also the displacement operator (right shift), but in the input operation is the stream extraction operator used in conjunction with the stream object cin. This is operator overloading (operator overloading). C++ system on "<" < "And" > > "They are reloaded, and they are used differently by the user in different situations. The "< < "And" > > "Is placed in the header file stream. Therefore, if you want to use "<" in the program < "And" > > "For the stream insertion operator and the stream extraction operator, the header file stream(" using namespace std", of course) must be included in this file module.
The question now is whether users can override the operators provided by C++ to their own advantage, giving them new meanings and making them more versatile. For example, can I add two complex Numbers with a "+" sign? In C++, the operator "+" cannot be used directly in a program to add complex Numbers. Users must figure out how to add complex Numbers themselves. For example, the user can add complex Numbers by defining a special function.
Add complex Numbers by function.
#include <iostream>
using namespace std;
class Complex // define 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); // Declare the addition function of complex Numbers
void display( ); // Declare output function
private:
double real; // Real component
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 the output function
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main( )
{
Complex c1(3,4),c2(5,-10),c3;// define 3 Number of complex objects
c3=c1.complex_add(c2); // Call the complex sum function
cout<<"c1="; c1.display( );// The output c1 The value of the
cout<<"c2="; c2.display( );// The output c2 The value of the
cout<<"c1+c2="; c3.display( );// The output c3 The value of the
return 0;
}
The results are as follows:
c1=(3+4i)
c2=(5-10i)
c1+c2=(8,-6i)
The result is undoubtedly correct, but the invocation is not intuitive. It is too cumbersome and makes people feel very inconvenient. Can you also use the plus sign "+" to perform complex operations, just like the addition of integers? Such as
c3=c1+c2;
The compiler automatically adds the complex Numbers c1 and c2. If it can be done, it provides great convenience for the operation of the object. This requires overloading the operator "+".