[C ++] heavy-duty operators

Source: Internet
Author: User

The so-called overload is to give a new meaning. Function Overloading is to assign a new meaning to an existing function to implement new functions.
Operators can also be overloaded.
Operator Overloading is to give multiple meanings to existing operators.
Necessity
In C ++, the pre-defined operator can only operate on basic data types, but does not apply to user-defined types (such as classes)
(1) c ++ does not allow users to define new operators themselves. It can only overload existing C ++ operators.
(2) The vast majority of operators in the C ++ operator C ++ that allow overloading are allowed.
There are only five operators that cannot be overloaded:
(1) member access operators.
(2) Scope operator limit
(3) conditional operators? :
(4) member pointer operator *
(5) Compile the start symbol of the preprocessing command #
(3) The number of operators (that is, the number of operands) cannot be changed during the overload operation.
(4) Overloading cannot change the priority of operators.
(5) Functions with heavy-duty operators cannot have default parameters. Otherwise, the number of operator parameters is changed, which is in conflict with the preceding (3.
(6) The overloaded operator must be used together with a user-defined custom object. Its Parameter must have at least one class Object (or Class Object reference ). That is to say, parameters cannot all be standard types of c ++ to prevent users from modifying the properties of operators used for standard data.
(7) The operators used for class objects must be reloaded, but there are two exceptions. The operators "=" and "&" do not need to be reloaded.
① The Value assignment operator (=) can be used for every class object and can be used to assign values to each other among similar objects.
② The address operator does not need to be overloaded. It can return the starting address of the class object in the memory.
In short, when the original operator of C ++ is overloaded, its original semantics does not disappear, which is equivalent to defining a new operator for a specific class.
You can use member functions and friend functions to overload operators. You can refer to the following experiences:
(1) operators that can only be overloaded by member functions include =, (), [],->, new, and delete.
(2) it is best to reload a single object operator as a member function.
(3) compound assignment operators such as + =,-=, * =,/=, & =, and ,! = ,~ =, % =, >=, <=We recommend that you reload the function as a member function.
(4) For other operators, we recommend that you reload them as friend functions.
The method to overload operators is to define a function that overload operators. When you need to execute an overloaded operator, the system automatically calls this function to implement corresponding operations. That is to say, Operator Overloading is implemented by defining functions. Operator Overloading is essentially a function overloading. The Function Format of the overload operator is as follows:
Operator name of function type (table of parameters)
{
Reload operator Processing
}
When the reload is a class member function, the number of parameters = the number of original operations-1
(Except for + + and -- On the rear)
When the overload is a friend function, the number of parameters = the number of original operations, and at least one custom type parameter should exist.
Reload "+" and "-" operations as member functions of the plural class.
Rules:
The real and virtual parts are added and subtracted respectively.
Operands:
Both operands are complex objects.
# Include <iostream>
Using namespace STD;
Class complex // plural class declaration
{
Public: // external interface
Complex (double r = 0.0, double I = 0.0)
{
Real = R;
Imag = I;
} // Constructor
Complex operator + (complex C2); // + reload as a member function
Complex operator-(complex C2); //-Reload as a member function
Void display (); // The plural value of the output.
PRIVATE: // Private Data Member
Double real; // real part of the plural number
Double imag; // The Plural imaginary part.
};
Complex complex: Operator + (complex C2) // overload function implementation
{
Complex C;
C. Real = real + c2.real;
C. imag = imag + c2.imag;
Return complex (C. Real, C. IMAG );
}
Complex complex: Operator-(complex C2) // overload function implementation
{
Complex C;
C. Real = real-c2.real;
C. imag = imag-c2.imag;
Return complex (C. Real, C. IMAG );
}
Void complex: Display ()
{Cout <"(" <real <"," <imag <"I" <")" <Endl ;}
Void main () // Main Function
{Complex C1 (), C2 (), C3; // declares the object of the plural class
Cout <"C1 ="; c1.display ();
Cout <"C2 ="; c2.display ();
C3 = c1-c2; // perform the complex Subtraction using the overload Operator
Cout <"C3 = c1-c2 = ";
C3.display ();
C3 = C1 + C2; // use the overload operator to complete the plural Addition
Cout <"C3 = C1 + C2 = ";
C3.display ();
}
The output result of the program is:
C1 = (5, 4I)
C2 = (2, 10i)
C3 = c1-c2 = (3,-6i)
C3 = C1 + C2 = (7, 14i)
In this program, the operator "+", "-" is overloaded so that it can be used to add or subtract two complex numbers. In this example, the operator overload function operator + is used as a member function in the complex class.
"+", "-" Is a binary operator. Why is there only one parameter in the overloaded function in the program? In fact, the operator overload function has two parameters. Since the overload function is a member function in the complex class, one parameter is implicit, and the operator function is a member of the class Object implicitly using the this pointer.
As you can see, the overload function operator + accesses the members of two objects. One is a member of the object pointed to by this pointer, and the other is a member of the object of the form parameter. For example, this-> real + c2.real, and this-> real is c1.real.
After you reload an operator function as a member function, if an expression containing this operator, such as C1 + C2, is displayed, the compilation system interprets it as c1.operator + (C2)
That is, call the operator to overload the function through object C1, and use the second parameter in the expression (Class Object C2 on the right of the operator) as the real parameter of the function. The Return Value of the operator overload function is of the complex type, and the return value is the sum of the plural C1 and C2 (complex (c1.real + c2.real, c1.imag + c2.imag )).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.