How to operate the C ++ Operator

Source: Internet
Author: User

In C ++, each command in the command system has a C ++ operator, which indicates the nature of the operation that the command should perform, different commands are represented by different codes of the operator field. Each encoding represents an instruction.

Operator is used to operate data. Operator for computation, equality check, assignment, operation variables, and other strange work. C ++ has many operators. I don't want to list them all here. I only want to list the most common operators, as shown in the following table. Table 1.2 examples of commonly used C ++ operators:

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

It can be seen that this list is longer and cannot be remembered at once. When using the C ++ operators, 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 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;  

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 the C ++ operator 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 operators. Readers do not have to memorize the role of each operator, but can understand its role through programs and code segments in learning.

  1. Differences between standard input implementation methods in C and C ++
  2. How does the C ++ compiler allocate storage space for Const constants?
  3. Basic Conception and method of C ++ Class Library Design
  4. Several Methods for converting C ++ Language
  5. How to better compile C ++ code

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.