The C++ community is loath to add new keywords because they could conflict with existing code. That is why the standards committee has, for example, repurposed the auto keyword and provided more than one use for others, such as virtual and delete. C++11 has implemented another way to avoid adding keywords, and that is to use identifiers with special meanings. These identifiers, override and final, are not keywords, but they are used to implement language features. The compiler uses the context to determine whether they are used as ordinary identifiers or as language features:
class F
{
int final; // #1
public:
...
virtual void unfold() {...} = final; // #2
};
Here the final on line #1 is used as an ordinary identifier, and the final on line #2 is used to invoke a language feature. The two uses do not conflict with one another.
Also C++ has many identifiers that commonly appear in programs but that are not reserved. These include header file names, library function names, and main, the name of the required function with which execution begins. As long as you avoid namespace conflicts, you can use these identifiers for other purposes, although there is no reason to do so. That is, nothing except a lack of common sense prevents code like the following:
// allowable but silly
#include
int iostream(int a);
int main ()
{
std::
cout << iostream(5) << '\n';
return 0;
}
int iostream(int a)
{
int main = a + 1;
int cout = a -1;
return main*cout;
}
C. The ASCII Character Set
Computers store characters by using numeric codes. The ASCII (American Standard Code for Information Interchange) code is the most commonly used code in the United States. It’s also a subset (a very small subset) of Unicode. C++ lets you represent most single characters directly by including the character in single quotation marks, as in 'A' for the A character. You can also represent a single character by using the octal or hex code preceded by a backslash; for example, '\012' and '\0xa' both represent the linefeed (LF) character. Such escape sequences can also be part of a string, as in "Hello,\012my dear".
Table C.1 shows the ASCII character set in various representations. When used as a prefix in the table, the ^ character denotes using the Ctrl key.
Table C.1. ASCII Character Set Representations
D. Operator Precedence
Operator precedence determines the order in which operators are applied to a value. C++ operators come in 18 precedence groups, which are presented in Table D.1. Those in Group 1 have the highest precedence, those in Group 2 have the next-highest precedence, and so on. If two operators apply to the same operand (something on which an operator operates), the operator with the higher precedence applies first. If the two operators have the same precedence, C++ uses associativity rules to determine which operator binds more tightly. All operators in the same group have the same precedence and the same associativity, which is either left-to-right (L–R in the table) or right-to-left (R–L in the table). Left-to-right associativity means to apply the leftmost operator first, and right-to-left associativity means to apply the rightmost operator first.
Table D.1. C++ Operator Precedence and Associativity
Some symbols, such as * and &, are used for more than one operator. In such cases, one form is
The following are some examples of precedence and associativity.
Here’s an example in which the compiler has to decide whether to add 5 to 3 or multiply 5 by 6:
3 + 5 * 6
The * operator has higher precedence than the + operator, so it is applied to the 5 first, making the expression 3 + 30, or 33.
Here’s an example in which the compiler has to decide whether to divide 6 into 120 or multiply 6 by 5:
120 / 6 * 5
Both / and * have the same precedence, but these operators associate from left to right. That means the operator to the left of the shared operand (6) is applied first, so the expression becomes 20 * 5, or 100.
Here’s an example in which the compiler has to decide whether to begin by incrementing str or by dereferencing str:
char * str = "Whoa";
char ch = *str++;
The postfix ++ operator has higher precedence than the unary * operator. This means the increment operator operates on str and not *str. That is, the operation increments the pointer, making it point to the next character, rather than altering the character pointed to. However, because ++ is the postfix form, the pointer is incremented after the original value of *str is assigned to ch. Therefore, this expression assigns the character W to ch and then moves str to point to the h character.
Here’s a similar example: