Understanding and running C++ programming, which is the standard for object-oriented languages, is easier when you know the expressions, declarations, and operators to perform calculations.
Expressions and Declarations in C++ Programming
Expressions take one of the following forms:
Literal expressions
A literal is a form of constant expression. The various types of literals are defined in the following table.
Declarations
Declarations use both intrinsic and user-defined types. The intrinsic types are
Declarations have one of the following forms:
The keyword auto
can be used if C++ can determine the type of variable itself:
The keyword decltype
extracts the type of an expression. This type can then be used wherever a type name is used. For example, the following example uses decltype to declare a second variable with same type as an existing variable:
A function definition has the following format:
An overloaded operator looks like a function definition. Most overloaded operators may be written either as member or simple functions. When written as a member function, *this
is the assumed first argument to the operator:
Users may also define their own types using the class
or struct
keywords:
In addition, a constructor with a single argument may be flagged as explicit
meaning that it will not be used in an implicit conversion from one type to another. Flagging a constructor as default
means "use the default C++ constructor definition". Flagging a constructor as delete
removes the default C++ constructor definition.
C++ supports two types of enumerated types. The following old enumeration type does not create a new type:
By default an individual entry is of type int
but this can be changed in the C++ 2011 standard:
C++ 2011 allows a second format that does create a new type:
Template declarations have a slightly different format:
Operators in C++ Programming
All operators in C++ perform some defined function. This table shows the operator, precedence (which determines who goes first), cardinality, and associativity in the C++ program.
Flow Control in C++ Programming
The following C++ structures direct the flow of control through the program. If you're an experienced programmer, the function of these structures will be familiar from other languages.
IF
The following command evaluates booleanExpression
. If it evaluates to true
, then control passes to expressions1
. If not, then control passes to the optional expressions2
.
WHILE
The following command evaluates booleanExpression
. If this evaluates to true
, then control passes to expressions
. At the end of the block, control passes back to booleanExpression
and repeats the process.
DO…WHILE
The following command executes expressions
. It then evaluates booleanExpression
. If this evaluates to true, control returns to the top of the loop and repeats the process.
FOR
The following command executes initCommand
which may be an expression or a variable declaration. It then evaluates boolExpression
. If this evaluates to true
, then control passes to expressions1
. If boolExpression
is false
, then control passes to the first statement after the closed brace of the for
loop. Once expressions
completes, control passes to the expression contained in loopExpression
before returning to boolExpression
to repeat the process. If initCommand
declares a new variable, it goes out of scope as soon as control passes outside of the loop.
FOR (EACH)
The 2011 standard introduces a second form of for
loop sometimes known as a "for each" because of its similarity to the foreach
found in some other languages. In this form, the variable declared in declaration
takes the value of the first member of list
and executes the expressions
block. When complete, the declared variable takes the second value of list
and executes expressions
again. This process is repeated for each value in list
.
SWITCH
The following command evaluates integerExpression
and compares the result to each of the cases listed. If the value is found to equal one of the constant integral values, val1
, val2
, etc., control passes to the corresponding set of expressions and continues until control encounters a break
. If expression does not equal any of the values, control passes to the expressionsN
following default
.
BREAK, CONTINUE, GOTO
A continue
passes control to the end of the closed brace of any of the looping controls. This causes the loop to continue with the next iteration. For example, the following loop processes prime numbers between 1 and 20:
A break
passes control to the first statement after the closed brace of any of the looping commands. This causes execution to exit the loop immediately. For example, the following reads characters until and end-of-file is encountered:
A goto label
passes control to the label provided. The break example above could have been written as follows: