1.6 C++ binocular operator overloading

C++ binocular operator overloading

Binary operator overloading in C++ refers to overloading binary operators, that is, operators with two operands, such as addition, subtraction, multiplication and division operators "+", "-", "*" and "/".

By overloading the binocular operator, the operator operation of the user-defined type can be realized.

For example, you can implement custom types of vector addition and subtraction operations by overloading addition and subtraction operators, or implement custom types of matrix multiplication operations by overloading multiplication operators.

The format of C++ binocular operator overloading is:
insert image description here
where, the return type can be any legal data type, the parameter list contains at least one parameter, and there can be multiple parameters.

C++ member function implements binocular operator overloading

If a binary operator overload is implemented as a member function of a class, the overloaded function has only one parameter, the other operand.

Write a member function demo that overloads the addition operator. The source code is as follows:
insert image description here
In this demo, I define a constructor and a member function that overloads the addition operator in the Complex class.

The constructor is used to initialize the real part and imaginary part of the complex number, and the member function of the overloaded addition operator is used to implement the addition operation of the complex number and return the result.

The class also defines a display function for outputting the value of complex numbers.

Compilation and running results:
insert image description here
C++ non-member functions implement binocular operator overloading

If a binary operator overload is implemented as a non-member function, the overloaded function takes two parameters, which are the two operands.

I wrote a non-member function demo that overloads the multiplication operator. The source code is as follows:
insert image description here
insert image description here
insert image description here
In this demo, I defined a constructor, a copy constructor, a destructor, an assignment operator overloading function and a A non-member function that overloads the multiplication operator.

The constructor is used to initialize the number of rows and columns of the matrix, and dynamically allocate memory.

The copy constructor is used to realize the deep copy of the matrix, and the destructor is used to release the dynamically allocated memory.

Assignment operator overloading functions are used to implement matrix assignment operations.

Non-member functions that overload the multiplication operator are used to perform matrix multiplication and return the result.

I also defined a display function in the class to output the value of the matrix.

In the main function, two matrices m1 and m2 are defined respectively, and they are multiplied to get m3. Finally, the values ​​of m1, m2 and m3 are output respectively.

Compile and run results:
insert image description here

Supongo que te gusta

Origin blog.csdn.net/qq_40240275/article/details/131200965
Recomendado
Clasificación