Wrox Press C++ Tutorial
Summary
In this chapter, we have covered the basics of computation in C++. We have learnt about all of the elementary types of data provided for in the language, and all the operators that manipulate these types directly. The essentials of what we have discussed up to now are as follows:
-
A DOS program in C++ consists of at least one function called
main().
-
The executable part of a function is made up of statements contained between curly braces.
-
A statement in C++ is terminated by a semicolon.
-
Named objects in C++, such as variables or functions, can have names that consist of a sequence of letters and digits, the first of which is a letter, and where an underscore is considered to be a letter. Upper and lower case letters are distinguished.
-
All the objects, such as variables, that you name in your program must not have a name that coincides with any of the reserved words in C++.
-
All constants and variables in C++ are of a given type. The basic types are
char, int, long, float, and double.
-
The name and type of a variable is defined in a declaration statement ending with a semicolon. Variables may also be given initial values in a declaration.
-
You can protect the value of a variable of a basic type by using the modifier
const. This will prevent direct modification of the variable within the program and give you compiler errors everywhere that a constant's value is altered.
-
By default, a variable is automatic, which means that it only exists from the point at which it is declared to the end of the scope in which it is defined, indicated by the corresponding closing brace after its declaration.
-
A variable may be declared as
static, in which case it continues to exist for the life of the program. It can only be accessed within the scope in which it was defined.
-
Variables can be declared outside of all blocks within a program, in which case they have global namespace scope. Variables with global namespace scope are accessible throughout a program, except where a local variable exists with the same name as the global variable. Even then, they can still be reached by using the scope resolution operator.
-
The Standard Library contains functions and operators which you can use in your program. They are contained in the namespace
std. This namespace is usually accessed with the using directive; individual objects in the namespace can be accessed by using the scope resolution operator.
-
An lvalue is an object that can appear on the left-hand side of an assignment. Non-
const variables are examples of lvalues.
-
You can mix different types of variables and constants in an expression, but they will be automatically converted to a common type where necessary. Conversion of the type of the right hand side of an assignment to that of the left-hand side will also be made where necessary. This can cause loss of information when the left-hand side type can't contain the same information as the right-hand side:
double converted to int, or long converted to short, for example.
-
The keyword
typedef allows you to define synonyms for other types.
Although we have discussed all the basic types, don't be misled into thinking that's all there is. There are more complex types based on the basic set as we shall see, and eventually you will be creating original types of your own.
© 1998 Wrox Press