C + + language base-c++ operator

Source: Internet
Author: User

Operator (operator) is used to manipulate data. Operators to compute, check the equation, perform assignments, manipulate variables, and do other, more bizarre work. There are many operators in C + + that do not want to list all, but list the most commonly used operators, as shown in the following table. Table 1.2 Examples of common C + + operator operators
Arithmetic operators
-Reduction of x=y-z;
* by X=y*z;
/except x=y/z;
Assignment operator
= assigned value x=10;
= = assigned Value and x+=10; (equal to x=x+10;)
-= assignment and minus x-=10;
*= assignment and multiply x*=10;
\= Assignment and addition of x\=10;
&= Assignment bit and x&=0x02;
|= assignment or x|=0x02;
logical operators
&& logic and if (x && 0xFF) {...}
|| Logical OR if (x | | 0xFF) {...}
Equality operator
= = equals if (x = = 10) {...}
!= is not equal to if (x!= 10) {...}
< less than if (X < 10) {...}
> Greater than if (X > 10) {...}
<= is less than or equal to if (x <= 10) {...}
>= is greater than or equal to if (x >= 10) {...}
Unary operator
* Indirect operator int x=*y;
~ Bit non x &=~0x02;
! Logical non-if (!valid) {...}
+ + increment operator X + + (equals x=x+1;)
--decrement operator x--;
Class and struct operators
:: Scope Analysis MyClass:: SomeFunction ();
-> Indirect member myclass-> SomeFunction ();
· Direct member MyClass. SomeFunction ();
As you can see, this list is a bit longer and cannot be remembered at all. When you use C + +, you will become familiar with these operators. It must be noted that the increment operator can be used either as a forward increment (++x) or as a post increment (x + +). The forward increment operator tells the compiler to increment and then use the variable, and then the increment operator lets the compiler use the value of the variable to increment it first. For example, the following code:
int x = 10;
cout << "x =" << x + + << end1;
cout << "x =" << x << end1;
cout << "x =" x << end1;
cout << "x =" << ++x << end1;
The output results are as follows:
x=10
x=11
X=12
X=12

Functions in C + +
A function is a code segment that is separate from the main program. These code segments are called (executed) when a particular action is required in the program. For example, a function might take two values and perform complex mathematical operations on them. The result is then returned, and the function may parse it with a string and return a portion of the parsed string. The new term function is the code segment that is separated from the main program to perform a predetermined service. Functions are an important part of various programming languages, and C + + is no exception. The simplest function, with no arguments, returns void (which means nothing is returned), and other functions may take one or several arguments and may return a value. The function name rule is the same as the variable name. Figure 1.5 shows the constituent parts of the function. The new term parameter (parameter) is the value passed to the function to change the operation or indicate the degree of operation.
return type function name parameter table
↓↓↓
int SomeFunction (int x, int y) {
function body →int z = (x * y); return z; ↑ RETURN Statement
}
Figure 1.5 The constituent parts of a function use a function before you declare it. A function declaration or prototype (prototype) tells the compiler function the number of arguments taken, the data type of each parameter, and the data type of the function return value. Listing 1.4 shows this concept. A new term prototype (prototype) is a declaration of a function's appearance or a description of its definition.
List 1.4muttiply.cpp
1: #include <iostream.h>
2: #include <conio.h>
3: #pragma hdrstop
4:
5:int Multiply (int,int)
6:void showresult (int);
7:
8:int Main (int argc,char **argv);
9:{
10:int X,y,result;
11:cout << end1 << "Enter": ";
12:cin >> x;
13:cout << "Enter The second value:";
14:cin >> y;
15:result=multiply (X,y);
16:showresult (result);
17:cout << end1 << end1 << "Press any key to continue ...";
18:getch ();
19:return 0
20:}
21st:
22:int multiply (int x,int y)
23: {
24:return x * y;
25:}
26:
27:void showresult (int res)
28: {
29:cout << "The result is:" << res <<end1;
30:}
The 11 to 14 lines of this program take two digits to the user using the standard input stream cin, and line 15th calls the multiply () function to multiply two numbers, and line 16th calls the Showresult () function to display the results of the multiplication. Note the prototype declaration of the 5th and 6th lines Multiply () and the Showresult () function in front of the main program. The prototype lists only the data types of the return type, function names, and functions parameters. This is the most basic requirement of a function declaration. The function prototype can also contain variable names for the function of the document 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 obvious, However, the code can be used to document both by description and by itself. Note that the definition of function multiply () (22 to 25 lines) in Listing 1.4 is outside the main function definition code segment (8 to 20 lines). The function definition contains the actual function body. The function body here is the most basic, because the function simply multiplies the two parameters of the function and returns the result. The function multiply () in Listing 1.4 can be invoked in several ways, passing the results of variables, direct numbers, or other function calls:
result = multiply (2,5);//passing literal values
result = Multiply (x,y); Passing variables
Showresult (Multiply (x,y));
return value used as a
Parameter for another function
Multiply (x,y);//return value ignored
Note that the return value is not used in the last example. In this case, calling the function multiply () without having to return a value makes no sense, but the return value is often ignored in C + + programming. There are many functions that take a specific action and then return a numeric value that represents the state of the function call. Sometimes the return value has nothing to do with the program and is negligible. If you ignore the return value, you simply discard the value without any other harm. For example, the return value of the Getch () function is omitted from the previous sample program (returns the ASCII value of the key). Functions can call other functions, even call themselves, called recursion (recursion). This is a more complex problem in C + + programming, not described here. The new term recursive (recursion) is the process by which the function calls itself. The functions described in this section refer to stand-alone functions in C or C + + programs (Independent functions are not members of a class). Standalone functions in C + + can be used in the same way as C, but C + + will further deepen the function and will be introduced later in C + +.

Related Keywords:
Related Article

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.