Analysis of C ++ Operators

Source: Internet
Author: User

The following describes a program of C ++ operators for operator overloading, so that it increases the variable by 10 instead of by 1. Operator overload is an advanced C ++ technology, in use, it will definitely bloat the eyes of many programmers. After reading this article, you will solve these problems.

See the following table. Examples of common C ++ operators:

 
 
  1. + AddX=Y+ Z;
  2.  
  3. -SubtractionX=Y-Z;
  4.  
  5. * MultiplicationX=Y* Z;
  6.  
  7. /DivisionX=Y/Z;
  8.  
  9. Value assignment operator
  10.  
  11. = Value assignmentX=10;
  12.  
  13. + = Value assignment and x + = 10; (equalXX= X + 10 ;)
  14.  
  15. -= Value assignment and SubtractionX-=10;
  16.  
  17. * = Value assignment and multiplication x * = 10;
  18.  
  19. \ = Assignment and Division x \ = 10;
  20.  
  21. & = Value assignment and x & = 0x02;
  22.  
  23. | = Assign a value or x | = 0x02;
  24.  
  25. Logical operators
  26.  
  27. & Logic and if (x & 0xFF ){...}
  28.  
  29. | Logic or if (x | 0xFF ){...}
  30.  
  31. Equality Operators
  32.  
  33. = Equal to if (X= 10 ){...}
  34.  
  35. ! = Not equal to if (x! = 10 ){...}
  36.  
  37. <Less than if (x< 10){...}
  38.  
  39. >Greater than if (x>10 ){...}
  40.  
  41. <= Less than or equal to if (x<= 10 ){...}
  42.  
  43. >= Greater than or equal to if (x>= 10 ){...}
  44.  
  45. Unary operator
  46.  
  47. * Indirect operator intX= * Y;
  48.  
  49. & Address operator int *X= & Y;
  50.  
  51. ~ Non-x ~ 0x02;
  52.  
  53. ! The logic is not if (! Valid ){...}
  54.  
  55. ++ Increment operator x ++ equalsXX= X + 1 ;)
  56.  
  57. -- Decrease operator x --;
  58.  
  59. Class and structure Operators
  60.  
  61. : Scope resolution MyClass: SomeFunction ();
  62.  
  63. ->Indirect member MyClass->SomeFunction ();
  64.  
  65. · Directly member MyClass. SomeFunction ();

It can be seen that this list is longer and cannot be remembered at once. When using C ++, you will gradually become familiar with these operators. It must be noted that the incrementing operator can be either a forward incrementing (++ x) or a post-incrementing (x ++ ). The increment operator tells the compiler to increment first and then use the variable. Then, the increment C ++ operator allows the compiler to increment first with the variable value. For example, the following code:

 
 
  1. int x = 10;   
  2.  
  3. cout << "x = " << x++ << end1;   
  4.  
  5. cout << "x = " << x << end1;   
  6.  
  7. cout << "x = " x << end1;   
  8.  
  9. cout << "x = " << ++x << end1;   
  10. x=10   
  11.  
  12. x=11   
  13.  
  14. x=12   
  15.  
  16. x=12   

This is also the case with the drop-down operator. I don't want to talk too much about this content here, but the reader can read it patiently, as Peng Zi told Augusta, "OGU, be patient, rome was not built in one day ". It indicates that operators in C ++ can be overloaded ).

Programmers can use standard overload operators to perform specific operations in specific classes. For example, you can use the overload increment operator in a class to increase the variable by 10 instead of by 1. Operator overload is an advanced C ++ technology, which is not described in detail in this book.

You may find that some operators use the same symbol. The meaning of a symbol varies with the situation. For example, an asterisk (*) can be used as a multiplication number, a pointer is declared, or a pointer reference is canceled. It seems a little messy at the beginning. In fact, C ++ Programmers sometimes feel a little messy. More practices, you will gradually adapt. There are many examples in this book to introduce these C ++ operators. Readers do not have to memorize the role of each operator, but can understand its role through programs and code segments in learning.

Functions in C ++

A function is a code segment separated from the main program. These code segments are called and executed when a specific action is required in the program ). For example, a function may take two values and perform complex mathematical operations on them. Then return the result. The function may take a string for analysis and return part of the analysis string.

The new term function) is a pre-defined service that is separated from the main program. Functions are an important part of various programming languages, and C ++ is no exception. The simplest function returns void without parameters.) Other functions may include one or more parameters and may return a value. The function name rule is the same as the variable name. Figure 1.5 shows the components of a function. The new term parameter is the value passed to the function, used to change the operation or indicate the operation degree.

  • About static members of C ++ class
  • How to use C ++ to encapsulate the Socket Library
  • Introduction to C ++ standard library
  • Introduction to C ++ standard library
  • C ++ code editing

Line 11 to 14 of this program uses the standard input stream cin to take two numbers to the user, line 3 calls the multiply () function to multiply the two numbers, and line 3 calls showResult () the function returns the result of multiplication. Note the prototype Declaration of the First 5th and 6th lines of the C ++ operator in the main program. Only the Data Types of the return type, function name, and function parameters are listed in the prototype.

This is the most basic requirement of function declaration. The function prototype can also contain the variable name used to document the function. For example, the function declaration of the multiply () function can be written as follows: int multiply (int firstNumber, int secondNumber); here, the function of multiply () is very effective, but the code can be both explained and documented by the Code itself. Note that the definition of the function multiply () in listing 1.4 (22 to 25 rows) is out of the main function definition code segment (8 to 20 rows.

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.