1. What is overloading?
A lot of what is said on the textbooks and on the Internet is obscure. In my own words:
C++ can have multiple definitions of functions and operators, similar to a word with multiple definitions. They are called function overloading and operator overloading.
You can use the function with the same name or the same operator to achieve different effects.
(1). Function overloading in C++
In the same scope, there can be a group of functions with the same name and different parameter lists. Their functions are similar, but their internal formal parameters must be different. We call this group of functions overloaded functions.
Role: We can reduce the number of function names by doing so and avoid the pollution of the name space. It is of great benefit to the readability of the program.
(2). Operator overloading in C++
In C++ language programs, we can redefine and overload many built-in operators, and I can implement custom functions through overloading. Overloaded operators must have functions with special names. Here we have a new keyword operator. Operator overloading must consist of the keyword operator and the operator symbol to be overloaded, and it needs a return type and a parameter list. As follows:
1 | ComplexNumber operator+(ComplexNumber z); |
Of course, there are also some operators defined in C++ cannot be overloaded:
The following is a list of operators that are non-overloadable:
Non-overloadable operator | statement |
---|---|
. | Member access operator |
.* and ->* | Member pointer access operator |
:: | Domain operator |
sizeof | Length operator |
?: | Conditional operator |
# | Preprocessing symbol |
2. Knowledge of complex numbers (partial)
(1). Definition and concept of plural
A complex number is a number expressed in the form of a + bi, where a and b are real numbers, and i can be said to be the solution of the equation x^2 = -1. Since no real number satisfies this equation, it is called imaginary number. For the complex number a + bi, a is called the real part, and b is called the imaginary part. Despite the historical term “fiction”, complex numbers are regarded as “real” as real numbers in mathematical sciences, and are the basis for many aspects of scientific descriptions in nature.
Complex numbers can solve certain equations, and these equations have no real number solutions.
For example, the equation (x+1)^2 = -9
This equation has no real number solution, because the square of a real number cannot be negative. But plurals provide a solution to this problem. It is to extend the real number with the uncertain i (sometimes called the imaginary unit) satisfying the relation i square = -1, so that the solution of the equation similar to the previous equation can be found. In this case, the solutions are -1 + 3i and -1-3i.
(2). The geometric meaning of complex numbers
Geometrically, complex numbers extend the concept of one-dimensional number lines to the two-dimensional complex plane by using the horizontal axis for the real part and the vertical axis for the imaginary part. The complex number a + bi can be identified by a point (a, b) in the complex plane. Complex numbers with zero real numbers are called pure imaginary numbers; the points of these numbers lie on the vertical axis of the complex plane. A complex number with zero imaginary part can be regarded as a real number; its point lies on the horizontal axis of the complex plane. Complex numbers can also be expressed in polar coordinates, which associate each complex number with its distance from the origin (its magnitude) and a specific angle (called the argument of the complex number).
Complex numbers can be represented visually as a pair of numbers(a, b),A vector is formed on the Argand graph, which represents a complex plane. “Re” is the real axis, “Im” is the imaginary axis, and i satisfies i^2 = -1.
Shows:
1.The complex number z=a+bi corresponds to the point (a, b) in the complex plane one to one
2. The complex number z=a+bi has a one-to-one correspondence with the vector OZ, where the coordinates of the Z point are (a, b)
(3). Addition and subtraction of complex algebraic forms
**Addition of two complex numbers:**:
$\ (x+yi)+(u+vi)=(x+u)+(y+v)i$
Two complex numbers subtraction rule:
$\ (x+yi)-(u+vi)=(x-u)+(y-v)i$
Of course, we can also understand the addition and subtraction of complex numbers through geometric forms. We can geometrically add two complex numbers by constructing parallelograms. Visualization using a complex plane: the sum of two complex numbers a and b is a point in the complex plane, which is obtained by constructing a parallelogram from three vertices O The points a and b of the arrow (provided they are not in a line). Equivalently, these points A and B are respectively called the fourth point X of the parallelogram. The triangles OAB and XBA are the same. The visualization of subtraction can be achieved by considering additional negatives.
(4). Multiplication and division of complex numbers
The rule of multiplication of complex numbers:
$\ (x+yi)*(u+vi)=(xu-yv)+(xv+yu)i$
The rule of division of complex numbers:
Here you need to use the conjugate complex number, that is, the two real parts are equal, and the imaginary parts are opposite to each other. When the imaginary part is not zero, the conjugate complex number means that the real part is equal and the imaginary part is opposite. If the imaginary part is zero, the conjugate complex number is itself. When the imaginary part is not equal to 0, it is also called conjugate imaginary number. The conjugate plural of the complex number z is denoted as z (plus a horizontal line, I can’t type this word if someone knows how to write it in a Markdown editor, can teach me), sometimes it can also be expressed as Z*. At the same time, the complex number z (plus one horizontal) is called the complex conjugate of the complex number z.
On the reciprocal calculation of non-zero complex numbers:
This can be used to represent the division of any complex number (z is not equal to 0)
3. Friend function in C++
A friend function is a function that is not a member of a class, but it can access all members of the class. It can be understood that the class grants a privilege to the friend function, so that the friend function can easily access and utilize all private and protected members.
Of course, the concept of friend can be a function or a class. We call it Friend Function and Friend Class. Our project of the friend category is currently not in use, so I will not analyze it carefully this time.
Function: If you are a developer, you see a project from someone else in the library, and you want to practice. In many cases, you cannot access the private and protected members of the code. If the function in the class has a friend function, it means that you can access private members and protected members in this friend function.
Key word:friend
If you want to declare a function as a friend of a class, you need to use the keyword friend before the function ** in the class definition, as shown below:
1 | friend ComplexNumber operator+(const float a, const ComplexNumber &z); |
4. Ideas and design
(1). Program composition and ideas
For the idea of the program, I first wrote a complex number class. In the private members of the class, I use double-precision floating-point numbers to define the real and imaginary parts. I will overload my own functions in public members. The real part and imaginary part can be input into the system. Here I have discussed three situations: both real and imaginary parts are input meaning; real and imaginary parts are not input; only imaginary part is input. And I initialize the real and imaginary parts to 0.
In the shared member, a function for adding real and imaginary parts is also written to realize the storage and conversion of real and imaginary parts. And the function of setting is the same. Then there is the function of the program output: internally I wrote a judgment statement to judge whether the imaginary part is greater than or equal to 0. The last is the function that overloads the four operations and the input and output operators. I will overload the addition in two cases. (It’s another matter to use class templates and so on. I will overload functions and operators here). I will set the input, output, and addition as friend functions so that they can directly access private members. Finally, write a test function statement outside the class. Just call it on the main function.
(2). Some functions correspond to the role of source code
When outputting, judge whether the imaginary part is greater than or equal to 0 to prevent negative situations such as negatives:
1 | void ComplexNumber::Show() |
Implementation of complex multiplication (overload of multiplication operator):
1 | ComplexNumber ComplexNumber::operator *(ComplexNumber z) |
Implementation of complex division operation (overload of division operator):
1 | ComplexNumber ComplexNumber::operator/(ComplexNumber z) |
Overloading of output operators:
1 | ostream & operator << (ostream & a,const ComplexNumber & z) |
Test input function:
1 | void CN() |
(3). Overall source code
The overall source code is as follows:
1 |
|
So far, “C++ language uses function overloading and operator overloading to complete complex number operations“ has been successfully implemented, thank you for reading.
This part is regarded as my study notes, which was first published on the CSDN platform and hoped to help more people. If you have any problems, I will correct them. Thank you very much for your comments and suggestions.
Copyright Notice
This article is the original content of Junhao except for the following references, and the final interpretation right belongs to the original author. If there is any infringement, please contact to delete. Without my authorization, please do not reprint it privately.
5. References
[1]. CppReference https://en.cppreference.com/w/cpp/language/operators
[2]. GeeksForGeeks https://www.geeksforgeeks.org/function-overloading-c/
[3]. Wikipedia https://en.wikipedia.org/wiki/Complex_number#Addition_and_subtraction
[4]. Runoob https://www.runoob.com/cplusplus/cpp-overloading.html
[5]. Runoob https://www.runoob.com/cplusplus/cpp-friend-functions.html
[6]. 360Doc http://www.360doc.com/content/19/0118/12/46601607_809660209.shtml