Chapter 2 native types and statements

Source: Internet
Author: User
Chapter 2 native types and
? 2.1 program elements
? 2.1.2 keywords
? 2.1.3 identifiers
? 2.1.4 literals
? 2.1.5 operators and punctuators
? 2.2 output/Input
? 2.3 program structure
? 2.4 simple types
? 2.4.1 Initialization
? 2.5 The traditional Conversions
? 2.6 enumeration types
? 2.7 expressions
? 2.8 statements
? 2.8.1 Assignment and expressions
? 2.8.2 the compound statement
? 2.8.3 the if
And if-Else
? 2.8.4 the while
? 2.8.5 The
? 2.8.6 The do
? 2.8.7 the break and continue
? 2.8.8 the switch statement
? 2.8.9 the GOTO statement
? 2.9 pragmatics
? Summary
? Exercises
Chapter 2 native types and
? This chapter will provide an introduction
Programming in C ++ using its native types
N At I V E T Y PE s
And its non-oop features.
? A native type is a type provided by
Language directly.
? A traditional imperative language such
Pascal or C. This is what we are calling
Kernel Language.
Ke r n e l an Gu age.
? An important feature of OOP is typet
Y pe-
E xt e n s I Bi L I t y.
2.1 program elements
? A program is composed of elements called
T oke n s,
Which are collections of Characters
That form the basic vocabulary the Compiler
? The character set nodes des:
? Lower-And uppercase alphabet: A B
? Z
? Digits: 0 1 2 3 4 5 6 7 8 9
? Operators: + * =... <.
? Punctuation :;,'....?
2.1 program elements
? Tokens can be interspersed with white space and
Comment text inserted for readability and
? There are five kinds of tokens:
? Keywords
? Identifiers
? Literals
? Operators
? And punctuators
? (See section C.2,
"Lexical elements," On page
406 ).
? In C ++,
A multiline comment is written
/* Possibly
Pos s I BL y
M u l t I L I n e
? C ++
Also has a single-line comment written
As // rest of line.
R e S t of L I n e.
2.1.2 keywords
? Keywords
K e y w or DS
Are explicitly reserved words that
Have a strict meaning in C ++.
? Words used for type declarations: int
, Char,
? Words used for statement Syntax: Do,,
And if
? Words used for access control: pub1i
And private.
2.1.2 keywords
2.1.3 identifiers
? An identifier is a sequence of letters, digits,
And underscores.
? An identifier cannot begin with a digit.
Upper-and lowercase letters are treated
? While in principle, identifiers can be
Arbitrarily long, invalid systems will
Distinguish only up to the first 3 1
2.1.3 identifiers
? Identifiers sample:
2.1.3 identifiers
? Illegal identifiers sample:
2.1.3 identifiers
2.1.4 literals
? Literals are constant values.
? There are literals for each c ++ native data
Types, including characters, booleans,
Integers, floating-point numbers, and
? String literals are also allowed.
2.1.4 literals
? Literal samples:
2.1.4 literals
? The character literals are usually given
'Symbol '.
2.1.4 literals
? Some nonprinting and special characters,
Such as blank or newline, require an escape
Sequence, as shown in table 2.8.
2.1.4 literals
? String literals are stored as a series
Characters terminated with the null
Character, whose value is 0. string literals
Are static char [] constants. The character '"'
Can be represented inside strings
Escaping it with the backslash character /.
2.1.4 literals
? Floating-point literals can be specified
Either with or without signed integer
Exponents, as partitioned strated by table 2.9.
2.1.5 operators and punctuators
? C ++ gives special meaning to convert
Characters and character sequences,
Replicated strated in Table 2.10.
2.1.5 operators and punctuators
? Operators are used in expressions and are
Meaningful when given appropriate
? Punctuators
P u n c t u at or S
Include parentheses, braces,
Commas, and colons, and are used
Structure elements of a program.
? Operators, punctuators, and white space
Serve to separate language elements.
2.2 output/Input
? Input/output is not directly a part of
Language. It is added as a set of types and
Routines found in a standard library.
? The iostream
I OS t r e AM
Library overloads the two bitshift
2.2 output/Input
? C ++ input/output is not directly part of
Language but rather is added as a set of types and
Routines found in a standard library. The C ++
Standard I/O Library is iostream or iostream. h.
File name without the. h extension is the official
ANSI standard name.
? Officially, the ANSI standard libraries that are
Taken from C libraries are c followed by their
Names without the. h extension. The ANSI C
Standard Library stdio. h can be used as the ANSI
C ++ library cstdio.
2.2 output/Input
? The iostream library overloads the two bitshift
? <// "Put to" output stream, normally left shift
?> // "Get from" input stream, normally right shift
? This library also declares three standard
? Cout // standard out
? Cin // standard in
? Cerr // standard error
? Demo on page 29: Io. cpp
2.3 program structure
? A program
In C ++
C +
Is a collection
Functions and declarations.
? Demo on page 30: GCD. cpp
? C ++ relies on an external standard library
Provide input/output.
? C ++ relies on an external standard library
Provide assertion testing.
2.3 program structure
? C ++ uses a Preprocessor to handle a set
? A c ++ program consists of declarations that
May be in different files.
? The function main ()
Is used as the starting
Point for execution of the program.
2.3 program structure
? The Assert
Macro tests a condition
Correctness, and terminates the program if
The test fails.
? C ++ compilers can compile Multifile
CC Module 1. c module2.c my-
C m odu l e 1. c m odu l e 2. c m y-
Main. c
M ai n. c
2.4 simple types
? The simple native types in C ++ are bool, Int,
Double, Char, and wchar_t.
? Bool
And the wchar_t types are new to C ++
And are not in C and early C ++ systems.
? The bool type provides a native Boolean
Type, and wchar_t provides a wide character
Type, used for representing character sets
Requiring more than the standard 255
2.4 simple types
? C ++ integral simple types can often be
Modified by the keywords short, long,
Signed, and unsigned, to yield further
Simple types.
? The floating-point types are float, double,
And long double.
2.4 simple types
? On most machines a bool or char is stored
In 1 byte. On memory PCs an int is stored in 2
Bytes, while 1ong, float, and double are all
Stored in 4 bytes.
? C ++ also has the sizeof operator, which is
Used to determine the number of bytes
Particle object or type requires
2.4 simple types
2.4 simple types
? The range of integral values that can be
Represented on your system is defined in
Standard header file limits. Some examples
From our system are shown in Table 2.12.
2.4 simple types
? The range of floating-point values that can
Be represented on your system is defined in
The standard header file float. Some
Examples from our system are already strated in
Table 2.13.
2.4 simple types
? The C ++ standard library file limits contains
The template numeric_limits, which allows
The user to query the system about
Characteristics of Different types.
Cout <numeric_limits <int>: max ()
<S the max of Int .?
2.4.1 Initialization
? A variable Declaration
De c l ar at I on
Associates a type
With a variable name.
? Demo on page 33: circle1.cpp
? The variables are declared and defined inside
Main (). Informally, we think of the definition
As creating the object.
? A definition can also initialize the value of
2.4.1 Initialization
? Initialization can involve an arbitrary
? Initialization makes the code more readable,
Less error prone, and more efficient.
2.5 The traditional Conversions
? If X
And y are of different types, then x
+ Y
Is a mixed
E xpr e s I on.
? Automatic expression conversion X op y
X op y
1. Any bool, Char, short, or enum is promoted to I
N t. integral types that cannot be represented
As I n t are promoted to unsigned.
2. If, after the first step, the expression is
Mixed Type, then, according to the hierarchy
2.5 The traditional Conversions
? The operand of lower type is promoted
That of the higher type, and the value of
Expression has that type.
? Int <unsigned <long <unsigned long <F L
O a t <double <long double
2.5 The traditional Conversions
? An automatic conversion can occur with
? A promotion,
Or widening,
Such as d
= I,
Will usually be well behaved, but
Or narrowing,
Such as I
= D,
Lose information.
? Eexplicit conversions called casts.
If I is
I nt
2.5 The traditional Conversions
Static_cast <double> (I)
? Will cast the value of I so that
Expression has type double.
? The static_cast
Itsc_is available for
Conversion that is well defined, portable,
And invertible.
2.5 The traditional Conversions
? Casts that are representation-or systemdependent
Use reinterpret_cast
? I
= Reinterpret_cast
> (& X) // system-dependent
? Two other special casts exist in C ++: const_cast
And dynamic_cast
? The const
Modifier means a variable's value is
? Older C ++ systems allow an unrestricted form
Cast with the following forms:
(Type) expression or type (expression)
2.5 The traditional Conversions
? Demo on page 37: m_to_k.cpp
? Where the inline keyword modifies a function
Definition, it suggests to the compiler that
When invoked, the Code defining it avoid
Function call, by being compiled inline.
2.6 enumeration types
? The keyword Enum is used to declare
Distinct Integer type with a set of named
Integer constants called enumerators.
E n u m e r at or S.
Enum suit {clubs, diamonds, hearts, spades };
? In C ++, the identifier s u I t is now its own
Unique type distinct from other integer
Types. This identifier is called Tag
T Ag
N am E.
2.6 enumeration types
? Enumerators can be defined and initialized
To arbitrary Integer constants.
Enum ages {Laura = 7, I R A, Harold = 59,
Philip = Harold + 7 };
2.6 enumeration types
? Demo on page 39: enum_tst.cpp
? Enumerators can be declared anonymously
An on y m ou s l y
Without a tag name.
? Enum {lB = 0, UB
= 99 };
? Enum {lazy, hazy, crazy} Why;
2.7 expressions
? C ++ has a variety of operators and
Expression forms.
? Assignment expression
? Arithmetic expression
? Toward mixing types and automatic Conversions
2.7 expressions
? Relational, relational ity, and logical expression
2.7 expressions
? Comma Operator
Expr1, expr2
? Conditional operator? :
Expr1? Expr2: expr3
? The parentheses are not necessary because
The conditional operator has precedence
Over the assignment operator.
? They operate on the machine-dependent bit
Representation of integral operands.
2.7 expressions
2.7 expressions
? C ++ considers function call
F u n C t I on C al l
() And Indexing
I n de xi n g
Or subscripting
S u bs c r I pt I n g
[] To be operators. It also has
An address
ADDR e s
& Operation and an indirection
I n di r e c t I on
Or dereferencing
De r e f e r e n C I n g
? C ++ also has a sizeof
Operator. It is
Important to obtaining an appropriate
Amount of storage for dynamically allocated
2.7 expressions
2.7 expressions
2.8 statements
? C ++
Has a large variety of statement types.
It uses the semicolon as a statement
2.8.1 Assignment and expressions
? The left-hand side must be an lvalue.
Lvalue is a location in memory where
Value can be stored or retrieved. Simple
Variables are lvalues.
? C ++ expressions can be guaranteed
Using either the comma operator or
Statement termination.
2.8.1 Assignment and expressions
? C ++
Allows multiple assignments in a single
? C ++
Provides assignment operators that
Combine an assignment and some other
? C ++
Also provides autoincrement (++) and
Autodecrement (--) Operators in both prefix
And Postfix form.
2.8.1 Assignment and expressions
? The Postfix form behaves differently
The prefix form by changing the affected
Lvalue after the rest of the expression is
? Demo on page 45: auto. cpp
? The null statement is written as a single
? Usually a null statement is used where
Statement is required syntactically, but no
Action is desired.
2.8.2 the compound statement
? A compound statement in C ++ is a series
Statements surrounded by the braces {and
The chief use of the compound statement
Is to group statements into an executable
? In C, when declarations come at
Beginning of a compound statement,
Statement is called a block.
Bl oc k.
2.8.3 the if
I f
And if-Else
? The general form of an if statement is
If (condition)
If (condition)
? Demo on page 46 if_test.cpp
2.8.4 the while
W h I l e
? The general form of a whi1e statement is:
While (condition)
? Demo on page 47: while_t.cpp
2.8.5 The
F or
? The general form of a for statement is
For (for-init-statement Condition
(F or-I n I t-s t at E m e n t C on di t I on
; Expression)
E xpr e s I on)
S t at E m e n t
Next statement
N e xt s t at E m e n t
? Demo on page 47: for_test.cpp
2.8.6 The do
? The general form is
S t at E m e n t
While (condition)
(C on di t I on)
Next statement
N e xt s t at E m e n t
? Demo on page 49: do_test.cpp
2.8.7 the break and continue
? To interrupt the normal flow of control within
Loop, the programmer can use the two special
? Break;
? Continue;
? The break statement, in addition to its use in
Loops, can also be used in a switch statement.
? The continue statement causes the current iteration
Of a loop to stop and the next iteration of the loop
To begin immediately.
2.8.8 the switch statement
? The general form of the switch statement is
Switch (condition)
? Demo on page 51: switch_t.cpp
2.8.8 the switch statement
? The effect of a switch
Iftsceaohetw 1. Evaluate the integral expression in
Parentheses following switch.
2. Execute the case label having a constant value
That matches the value of the expression found
In step 1; if no match is found, execute
Default label; if there is no default label,
Terminate the switch.
3. Terminate the switch when a break statement
Is encountered, or by "falling off the end ."
2.8.9 the GOTO statement
? GOTO statement General Form:
Goto label;
? Demo on page 52: goto_tst.cpp
2.9 pragmatics
? Tips 1
2.9 pragmatics
? Tips 2
2.9 pragmatics
? Tips 3
? Avoid casting, but where it is necessary use
The new-style safe casts such
ITSC _ <>.
? The use of const
And inline
Is to be stronugly
Preferred to the use of "equivalent" define
? 1.
C ++ program consists of declarations
That may be in different files.
? 2.
The function main
() Is used as
Starting point for execution of the program.
? 3. c ++ uses a Preprocessor to handle a set
Of ctictives, such as the include Directive,
To convert the program from its
Preprocessing form to pure C ++ syntax.
? 4. c ++ relies on an external standard library
To provide input/output.
? 5. The required simple types in C ++ are
Bool, double, I n t, Char, and wchar_t.
? 6. C ++ has a large variety of statement
Types. It uses the semicolon as a statement
? 7. The general form of an I f statement
? 8. The general form of a while statement
? 9. To interrupt normal flow of control
Within a loop
? 10. The GOTO statement is the most primitive
Method of interrupting ordinary Control

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: 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.