Column: VC Camp - C / C Column | Join Date: 2001-9-6 15:52:53 | Reading: 9 Turn off Window Operator Overload
Foreword
Polymorphism is one of the important features of object-oriented programming. It consists of the three major characteristics of the object-oriented programming with the previous packaged package and inheritance. These three characteristics are interrelated. The encapsulation is the foundation, the inheritance is the key, and the polymorphism is supplemented, and the polymorphism must exist in the inherited environment.
The so-called polymorphism refers to a complete different behavior when the same message is received by different types of objects. The message mentioned here is mainly to call the call of the members of the class, and different behavior refers to different implementations. With polymorphism, users only need to send general forms of messages, and leave all implementations to the object of the received message. Objects make corresponding actions (ie, operations) based on the received messages.
Function overload and operator overload are simple and polymorphism. The concept and usage of the function overload has been discussed in the "function overload", which is only a simple supplement, and we focus on the overload of the operator.
The so-called function is simply to assign multiple meanings to the same function name. In particular, C is allowed to define several different implementations in the same name in the same scope, which can be a member function or a non-member function. However, the parameters of the function are required to define this overload function or at least one type, or the number is different. And there is no requirement for the type of return value, which can be the same or different. The number of parameters and types are the same, and only different overload functions of returns are illegal. Because the compiler only considers the function table while selecting the same name, it means that the parameter number or parameter type difference is selected in the parameter table of the function.
It can be seen that the overload function is in that it can access a set of interrelated functions with the same name, and the compiler is selected, so this will help solve the program complexity problem. Such as: When defining a class, the constructor has brought a variety of ways to initialize and provides users with greater flexibility.
Let's focus on discuss operators overload.
The operator overload is given to the existing operator multi-impact. C can be redefined by redefining the operator, allowing it to perform specific functions for specific classes, which enhances the expansion capacity of the C language.
Several questions overloaded by operators
1. What is the role of operator overload?
It allows you to provide an intuition interface for users.
Operator overload allows C / C operators to have a user-defined meaning on the user-defined type (class). The overloaded operator is the syntax modification of the function called:
Class Fred
{
PUBLIC:
// ...
}
#if 0
// No operator overload:
Fred add (fred, fred);
Fred mul (fred, fred);
Fred f (Fred A, Fred B, Fred C)
{
Return Add (ADD (Mul (A, B), MUL (B, C)), MUL (C, A)); // Haha, more brunette ...
}
#ELSE
// Once an operator is overloaded:
Fred Operator (FRED, FRED);
Fred Operator * (Fred, Fred);
Fred f (Fred A, Fred B, Fred C)
{
RETURN A * B B * C C * a;
}
#ENDIF
2. What is the benefit of an operator overload?
By overloading standard operators, you can explore the user's intuition. The language used to make the user program is a problem, not the machine.
The ultimate goal is to reduce the learning curve and reduce the error rate.
3. Which operators can be used as an overload?
Almost all operators can be used as an overload. Specifically include:
Arithmetic operators: , -, *, /,%, , -; position operation operator: &, |, ~, ^, <<, >>
Logical operator:!, &&, ||;
Comparison operator: <,>,> =, <=, ==,! =
Assignment operator: =, =, - =, * =, / =,% =, & =, | =, ^ =, << =, >> =
Other operators: [], (), ->, (comma operator), new, delete, new [], delete [], -> *.
The following operators are not allowed to be overloaded:
.,. *, ::,?:
4. What should I do if the priority and conjunction occurs after the operator is overloaded?
The user overloads the new defined operator, does not change the priority and conjunction between the original operator. That is to say, the operator overload does not change the priority and combination of the operator, and after the operator is overloaded, the syntax structure of the operator is not changed, that is, the single operator can only be overloaded as a single operator, and the binocular operation The characters can only be overloaded with binocular operators.
5. Which operator function does the compiler choose?
The operator overload is actually a function, so the overload of the operator is actually the overload of the function. Compiler The selection of operator overloads follows the principle of selection of function overload. When encountering an unaptible operation, the compiler will look for the operator function that matches the parameter.
6. What are the restrictions on overload operators?
(1) Not a new operator. The overload operator must be limited to an operator that allows an overloaded operator in the existing operator in the C language.
(2) The overload operator insists on 4 "can't change".
· The number of operators cannot be changed;
· Cannot change the original priority of the operator;
· Cannot change the original combination of operators;
· Can't change the original grammatical structure of the operator.
7. What principles must be followed when operators are overloaded?
Operator overload allows the program more concise, enabling the expression more intuitive, increasing readability. However, the operator is not used to use too much, otherwise it will bring a certain amount of trouble.
The following principle should be followed when using the overload operator:
(1) The overload operator must be clear.
(2) The overload operator cannot be unsatisfactory.
Two forms of operator overload function
The function of operator overload is generally in the form of a member function form and friend function form. Both forms can access private members in the class.
1. Overloaded member functions
Here first, an example of four operators on the complex operation overloaded complex. The complex number is constructed from the real part and the imaginary portion, which can define a plurality of classes, and then overload the plurality of operators in the class. First look at the following source:
#include
Class Complex
{
PUBLIC:
Complex () {real = IMAG = 0;}
Complex (Double R, Double I)
{
REAL = R, IMAG = i;
}
Complex Operator (Const Complex & C);
Complex Operator - (Const Complex & C);
Complex Operator * (Const Complex & C);
Complex Operator / (Const Complex & C);
Friend Void Print; Const Complex & C);
Private:
Double Real, IMAG;
}
Inline Complex Complex :: Operator (Const Complex & C)
{
Return Complex (Real C.Real, IMAG C.IMAG);
}
Inline Complex Complex :: Operator - (Const Complex & C)
{
RETURN COMPLEX (Real - C.Real, IMAG - C.IMAG);
}
Inline Complex Complex :: Operator * (Const Complex & C)
{
Return Complex (Real * C.REAL - IMAG * C.IMAG, Real * C.Imag IMAG * C.REAL);
}
Inline Complex Complex :: Operator / (Const Complex & C)
{
Return Complex ((Real * C.Real Imag C.Imag) / (C.REAL * C.REAL C.IMAG * C.IMAG),
(iMag * C.REAL - REAL * C.IMAG) / (C.REAL * C.REAL C.IMAG * C.IMAG));
}
Void Print (Const Complex & C)
{
IF (C.IMAG <0)
COUT << C.Real << C.Imag << 'I';
Else
COUT << C.Real << ' ' << C.Imag << 'I';
}
void main ()
{
COMPLEX C1 (2.0, 3.0), C2 (4.0, -2.0), C3;
C3 = C1 C2;
COUT << "/ NC1 C2 =";
Print (C3);
C3 = C1 - C2;
COUT << "/ NC1-C2 =";
Print (C3);
C3 = C1 * C2;
COUT << "/ nc1 * c2 =";
Print (C3);
C3 = C1 / C2;
COUT << "/ nc1 / c2 =";
Print (C3);
C3 = (C1 C2) * (C1-C2) * C2 / C1;
COUT << "/ N (C1 C2) * (C1-C2) * C2 / C1 ="
Print (C3);
Cout << Endl;
}
The operation result of the program is:
C1 C2 = 6 1i
C1-C2 = -2 5i
C1 * C2 = 14 8i
C1 / C2 = 0.45 0.8i
(C1 C2) * (C1-C2) * C2 / C1 = 9.61538 25.2308i
In the program, class complex defines four member functions as operator overload functions. Describe the operator overload function to the membership function format of the class:
Where Operator is a keyword that defines the operator overload function.
Expression of the program appears in the program:
C1 C2
The compiler will be explained as:
C1.Operator (C2)
Where C1 and C2 are objects of the Complex class. Operator () is an overload function of the calculation .
The operator overload function has only one parameter C2. It can be seen that when the overload is a member function, there is only one parameter. The parameters cannot be explicitly explicitly explicitly explicitly explicitly explicitly explicitly explicitly explicitly explicit. When the overload is a member function, a parameter is always hidden, which is the THIS pointer. The THIS pointer is a pointer to call the member function object. 2. Overload as a friend function
The operator overload function can also be a friend function. When the reloader function, there is no implied parameter THIS pointer. In this way, there is 2 parameters for the binocular operators, the friend function, and a parameter for the single operator, the friend function. However, some operators cannot overload as a friend function, they are: =, (), [], and->.
The definition format of the operator overload function of the heavy-duty as a friend function is as follows:
Friend
{...}
The following uses friends function code member function, overloads the above example, the program is as follows:
#include
Class Complex
{
PUBLIC:
Complex () {real = IMAG = 0;}
Complex (Double R, Double I)
{
REAL = R, IMAG = i;
}
Friend Complex Operator (Const Complex & C1, Const Complex & C2);
Friend Complex Operator - (Const Complex & C1, Const Complex & C2);
Friend Complex Operator * (Const Complex & C1, Const Complex & C2);
Friend Complex Operator / (Const Complex & C1, Const Complex & C2);
Friend
Void Print; Const Complex & C);
Private:
Double Real, IMAG;
}
Complex Operator (Const Complex & C1, Const Complex & C2)
{
Return Complex (C1.Real C2.Real, C1.Imag C2.IMAG);
}
Complex Operator - (Const Complex & C1, Const Complex & C2)
{
Return Complex (C1.Real - C2.Real, C1.Imag - c2.imag);
}
Complex Operator * (Const Complex & C1, Const Complex & C2)
{
Return Complex (c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag c1.imag * c2.real);
}
Complex Operator / (Const Complex & C1, Const Complex & C2)
{
Return Complex ((c1.real * c2.real c1.imag c2.imag) / (C2.Real * C2.Real C2.Imag * c2.imag),
(c1.imag * c2.real - c1.real * c2.imag) / (c2.real * c2.real c2.imag * c2.imag));
}
Void Print (const complex & c) {
IF (C.IMAG <0)
COUT << C.Real << C.Imag << 'I';
Else
COUT << C.Real << ' ' << C.Imag << 'I';
}
void main ()
{
COMPLEX C1 (2.0, 3.0), C2 (4.0, -2.0), C3;
C3 = C1 C2;
COUT << "/ NC1 C2 =";
Print (C3);
C3 = C1 - C2;
COUT << "/ NC1-C2 =";
Print (C3);
C3 = C1 * C2;
COUT << "/ nc1 * c2 =";
Print (C3);
C3 = C1 / C2;
COUT << "/ nc1 / c2 =";
Print (C3);
C3 = (C1 C2) * (C1-C2) * C2 / C1;
COUT << "/ N (C1 C2) * (C1-C2) * C2 / C1 ="
Print (C3);
Cout << Endl;
}
The operation result of the program is the same as the above example. As mentioned earlier, when the operator, the overload is a member function, only one parameter, the other is implicit; the overload is a friend function, there are two parameters, there is no implicit parameter. Therefore, there is a procedure
C1 C2
The compiler is explained in:
Operator (C1, C2)
Call the following functions, make the value,
Complex Operator (Const Coplex & C1, Const Complex & C2)
3. Comparison of two overload forms
Generally speaking, the single operator is best to be overloaded as a member; it is better to be a friend function for the double-purpose operator, and the double-purpose operator overload is more convenient for the member function than the reproduction. However, Some binocular operators are still heavy. For example, the assignment operator is assigned. Because, if it is overloaded as a friend function, it will appear to the place where the assignment is inconsistent.
Other operator overload example
1). Subload operator overload
Since the size of the C language does not save its size, the array element cannot be checked to access the array element, and it is impossible to ensure that the array dynamic assignment will not be off. Use C classes to define a safer and powerful array type. To do this, the heavy-duty operator is defined for this class [].
Let's take a look at an example:
#include
Class CharRay
{
PUBLIC:
Chararray (int L)
{
Length = L;
BUFF = New char [length];
}
~ Charrray () {delete buff;
INT getLength () {return longth;}
Char & Operator [] (INT I);
Private:
Int length;
Char * buff;
}
Char & Chararray :: Operator [] (INT i)
{
Static char CH = 0;
IF (i
Return buff [I];
Else
{
Cout << "/ NINDEX OUT OF RANGE."; RETURN CH;
}
}
void main ()
{
int CNT;
Chararray string1 (6);
CHAR * STRING2 = "String";
For (CNT = 0; CNT <8; CNT )
String1 [CNT] = String2 [CNT];
Cout << "/ n";
For (CNT = 0; CNT <8; CNT )
COUT << string1 [cnt];
Cout << "/ n";
COUT << string1.getlength () << endl;
}
The advantages of the array class are as follows:
(1) It is not a constant.
(2) Running Dynamic Specify size can not be operated NEW and DELETE.
(3) When using such an array for function parameters, it is noted that the array variable itself and the size thereof respectively, because the size has been saved.
Attention should be noted when the subscript operator function is overloaded:
(1) This function can only bring one parameters and cannot bring multiple parameters.
(2) Do not overload as a friend function, must be a member of the non-Static class.
2). Overloaded 1 minus 1 operator
Increase 1 minus 1 operator is a single operator. They have two kinds of prefixes and suffixes. To distinguish these two computments, the suffix operation is considered an operator. expression
Obj or obj -
It is seen as:
Obj 0 or Obj - 0
The following example shows the application of overloaded 1 minus 1 operator.
#include
Class Counter
{
PUBLIC:
COUNTER () {v = 0;}
Counter Operator ();
Counter Operator (int);
Void print () {cout << v << endl;}
Private:
UNSIGNED V;
}
Counter Counter :: Operator ()
{
V ;
RETURN * THIS;
}
Counter Counter :: Operator (int)
{
Counter T;
T.V = V ;
Return T;
}
void main ()
{
Counter C;
For (int i = 0; i <8; i )
C ;
C.Print ();
For (i = 0; i <8; i )
C;
C.Print ();
}
3). Overload function call operator
You can see the function call operator () as an extension of the subscript operation []. The function call operator can bring 0 to multiple parameters. The following is aware of an instance to be familiar with the overload of the function call operator.
#include
Class F
{
PUBLIC:
Double Operator () (Double X, Double Y) Const;
}
Double f :: Operator () (double x, double y) const
{
Return (x 5) * y;
}
void main ()
{
F F;
Cout << f (1.5, 2.2) << ENDL;
}
2001-9-1 11:42