------operator Overloading of C + +

Source: Internet
Author: User
Tags bitwise operators

① What is operator overloading?

What is the operator overload of C + +?

It is actually the operator that gives it a new meaning or multiple meanings. Let it have another kind of new function.

Why do I need operator overloading?

In order to implement the polymorphism of the class in object-oriented, we refer to the operator overloading

"+", "-" can be used in the program to add and subtract integers, single precision, double precision, and pointers.

For example:

1 int 1+2// pair of integers plus 2double1.0+2.54// for double-precision plus 3 int por[]; 4 int *p = por; 5 1 // minus 1 on the pointer

② operator Overloading origin

in the computer's memory, the integer and the floating-point number storage form is different, the computer their addition and subtraction operation processing is dissimilar,

And the pointer-1 is not a simple address-1. In C + +, the compiler handles these problems, which can be based on the types on either side of an expression

Determine what to do with their operations, and finally decide what to do. So in C + +, operators can operate on different types of operations.

The problem in reality: the operation of complex numbers-------

Many children's shoes may have forgotten what a plural is. Let's first say what is plural (Optics Valley's learning to old live to old)

    A plural is a number a+bi that can be written as follows, where a and b are real numbers, and I are imaginary units. In complex A+bi, A is called the real part of a complex number, B is said

is the imaginary part of the complex number, I is called the imaginary unit. When the imaginary part equals zero, the complex number is the real number, and when the imaginary part is not equal to zero, the complex number is called the imaginary number.

If the real part equals zero, it is called a pure imaginary. From the above, the complex set contains the set of real numbers and is the expansion of the set of real numbers. The plural is by the Italian Milan scholar

Cadan was first introduced in 16th century and has been gradually accepted by mathematicians through the work of Moivre, Zeon, Euler, Gauss, and others. (From: Baidu Encyclopedia)

OK, here's the point:

For example, two plural: s1= (1+2i) s2= (6+1.2i)

We do two complex s=s1+s2= (7+3.2i) in mathematics (the real and real parts, the imaginary and imaginary parts add up)

How do we calculate in C + +?

All of the overloads of the operator that we need to use. 

Operator overloading format in ③ programs

The overloaded operator function syntax is:

The functions of overloaded operators are generally formatted as follows:
< return value type ># operator operator name (formal parameter table column)
{
Overloaded handling of operators
}

Operator is a keyword that is specifically used to define overloaded operator functions, #代表要被重载的运算符. Such as:

Complex operator + (const complex& A, const complex& b) {...} This function can be used to implement the addition of two Complex objects,

 

Analogy to the following example

1 intMain () {2Complex A (Ten,7), B (3,5);3Complex c= A +b;4Complex d = A-b;5Complex E = a + B-D;6cout <<"c="; C.print_complex ();7cout <<"d="; D.print_complex ();8cout <<"e="; E.print_complex ();9     intx= -, y= $, Z;Tenz = x +y; Onecout <<"z="<<Z; A}

Use the member function to overload the complex +,-operator operation

1#include <iostream>2 using namespacestd;3 classComplex {4 Private:5     DoubleRpart, IPART;6  Public:7Complex () {rpart=ipart=0.0; }8Complex (DoubleRpDoubleIP) {9RPART=RP; ipart=IP;Ten     } OneComplexoperator+(ConstComplex &c) { AComplex temp (Rpart+c.rpart, ipart+C.ipart); -         returntemp; -     } the      -Complexoperator-(ConstComplex &c); -     voidPrint_complex () -     { +cout<<"("<< rpart<<','<<ipart <<"i)"<<Endl; -     } + }; A //Out-of- class implementation of operator– functions atComplex Complex::operator-(ConstComplex &c) { -Complex temp (Rpart-c.rpart, ipart-C.ipart); -     returntemp; -}

Operation Result:

The C + + compiler is handled as (A.operator + (b)). operator– (d)

③ which operators can be overloaded?

What are the overloads of the operators?

Operators that can be overloaded

Monocular operator

+ (positive),-(negative), * (pointer), & (Take address)

Binocular arithmetic operators

+ (plus),-(minus), * (multiply),/(except),% (take touch)

Self-increment auto-decrement operator

+ + (self-increment) 、--(self-reduction)

logical operators

|| (logical OR), && (logic and),! (Logical non)

Relational operators

= = (equals),! = (not equal), < (less than), <= (less than equals), > (greater than), >= (greater than or equal)

Bitwise operators

| (bitwise OR), & (Bitwise AND), ~ (bitwise reversed), ^ (bitwise XOR), << (shift left), >> (right Shift)

Assignment operators

=, + =,-=, *=,/=,%=, &=, |=, ^=, <<=, >>=

Memory Request and release

New, delete[], new[], delete[]

Other operators () (function call),--(member access),->* (member pointer access), (comma), [] (subscript)

Class member access operator:->

Take a look at an example on the Internet:

1 #include <iostream> 2 using namespace std; 3 class a{4 public:5     A (double v) {6         value = V; 7     } 8     print () {9         cout << value;10     }11 Priva Te:12     Double value;13};14 class a_ptr{15 public:16     a_ptr (* p) {+         pa = p;18     }19 a     * operator-& gt; () {         pa;21     }22 private:23 a     * pa;24};

Here we direct compiler test output result:

Understanding: The A_ptr class encapsulates the Class A object, the statement "Aptr->print ();" In the "aptr->" section of the compiler called "A *aptr.operator, ();",

Returns a pointer to the object of Class A. Then call the print function of the object that the pointer points to. That is, the calling process is: (Aptr.operator-())->print ();

end!

Soon over the mid-Autumn festival, the company put three days, no sister paper, go out to play boring, then make good to fill the knowledge.

Also mention the programmer before wishing you a happy Mid-Autumn Festival, happiness and health!

I dick Silk one, no money to send the moon cakes everyone, send me a beautiful colleague photo to everyone to raise the eye-candy. Summer heat unbearable, may the beautiful woman bring a hint of cool feeling to everyone.

Summer is hot, do not wet! A recent news to everyone note: first-hand tour company programmer, Overtime death, only 25 years old. That's the thing: programmers should have less code at night and more rest. All health is important. Today also specializes in stewed a large rice cooker egg ribs soup to eat to fill the body. take a shower and have a rest .
Welcome to Exchange, sharing the programmer inspirational story. QQ Group of Happy programmers:

------operator Overloading of C + +

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.