| Main Menu for the Essentials of C++ |
| Next Section : Operators |
C++ is a typed language, meaning that all variables
and constants must be declared
using some description of the type
of information they will hold.
A. Simple Types
The built-in scalar C++ types include:
| Type | Size* | Range* | Comments |
| (in bits) |
|||
| int | 16 | -32768 to 32,767 | Most common integer type |
| long | 32 | -2,147,483,648 to 2,147,483,647 | Used when int is not large enough |
| float | 32 | 3.4 x 10-38 to 3.4 x 1038 | Real Numbers with 7 digit precision |
| double | 64 | 1.7 x 10-308 to 1.7 x 10308 | Real Numbers with 15 digit precision |
| char | 8 | -128 to 127 | For ASCII characters and small integers |
| enum | 16 | -32768 to 32,767 | Enumerated types |
*As found in Borland C++ 5.0
There is one other important type that more recent implementations of C++ include. This is the string. Although technically not a scalar type, it is included here because it is so much a part of many programs. See Section XIII and the document "The String Type in Borland C++" for more information.
B. Declaring Variables
Names for variables, constants, functions, classes
and user-defined types are called identifiers. All identifiers must be declared before they can be used in a program. Identifiers
must start with a letter or an underscore, '_', but digits can be
included elsewhere. The first 32 characters of an identifier are significant, meaning that any differences in the first 32 characters of two names will cause the compiler to treat the names as different. On the other hand, if the only difference between two names occurs after the first 32 characters, the names are the same as far as the compiler is concerned.
C++ is a case sensitive language meaning that the identifiers 'abc', ABC', and 'aBc' are all different.
Whitespace (spaces, tabs, newline characters and comments) indicates
the end of an identifier and therefore cannot be included in identifiers.
Thus 'mySpecialIdentifier' is a valid identifier but 'my Special
Identifier' is not. (The special capitalization here is just for
easy reading - read the "C++ Language Guidelines" for more information.)
A variable declaration consists of a type name, an identifier
name, and possibly an equal sign (=) followed by a value - to
initialize the variable. Valid declarations
include:
Note the semicolon at the end of each declaration. Although a
series of declarations can be grouped together with commas separating
them and a semicolon only at the end of the series, it is more
common to place them on separate lines ending in semicolons. Other
than being used as a separator whitespace is not significant.
Thus the second declaration above could have been written as:
In essence, whitespace is ignored except where it acts as a separator.
C. Declaring Constants
C++ programs can include specific integer, real, character, and
string values. Such values are called constants. Numeric
constants (integer and real constants) appear simply as values.
Character constants consist of single characters surrounded by
single quotes. String constants are some sequence of zero or more
characters surrounded by double quotes. Examples of constants include:
Note that 'a' is a character constant while "a" is a
string constant. A variable of type char could hold 'a' but not
"a". Also, '2' is a character while the digit 2 (not
surrounded by quote marks) is an integer. A space surrounded by
single quotes (' ') is character constant while " "
is a string constant representing one space. A string with nothing
in it, the null string, is represented by two adjacent
double quotes: "".
While it is not possible, by the very definition of a character, to have a character constant consisting of multiple blanks (or ANY sequence of letters or digits), strings can consist of any number of blanks or combinations of digits and letters. String constants cannot be broken up across multiple lines without the use of the backslash (\) character acting as a continuation character. Example:
Check a manual for more information on the use of special characters
such as tabs inside strings.
Often, constants are declared in programs
as named constants. The best
way to make such a declaration is by using the word const
followed by the type of the constant, the name (identifier) of
the constant, an equal sign, and its value:
If a type name is not supplied, 'int' is assumed as a default. It is considered good practice to add the type 'int' even though it is not necessary. Also for style and readability, named constants are best coded in upper case and with an underscore between words in a multiword constant identifier. (See the "C++ Language Guidelines".)
D. Lvalues
Variables and named constants represent memory locations and, as
such, can be used to 'locate' a specific memory location. In other
words, each such identifier is a symbolic name for a memory location.
There are other, more complex expressions, such as those involving
arrays and pointers,
that can also be used to name or designate specific memory locations.
All of these (variables, named constants, and expressions
that designate specific memory locations) are called lvalues – identifiers that represent symbolic names for memory.
Identifiers are the simplest form of an lvalue. Array and pointer
expressions represent more complex lvalues.
The word lvalue comes from 'left value' because such values originally
referred to identifiers or expressions that could legally stand
only on the left side of assignment statements. What were originally
lvalues we now call modifiable lvalues to distinguish them from
constants - which, of course, cannot be on the left side of assignment
statements.
In the statement:
if the identifier 'variable1' has been declared as a variable, it is a valid modifiable lvalue since it names a memory location in which the results of the expression on the right side (the rvalue) can be stored. However, the statement
would not be valid since "variable2 + variable3 * variable4" is not an lvalue. It does not name a specific memory location
in which to put the value on the right side of the expression.
E. Declarations vs. Definitions
A declaration by itself does not include the setting aside or allocation of memory for the identifier being declared. It is only a statement describing what may later come into existence in the computer's memory. A statement that sets aside memory is called a definition. Thus, the variable and constant declarations described here are really also definitions since memory is allocated. It is possible (using the 'extern' statement described in Section IV) to declare a variable without setting aside space for it. Function declarations are, however, often true declarations in that the definition is separate. Also, type declarations (Section IX and Section XI) do not set aside memory.
F. Keywords
C++ includes a set of words that are set aside with special meaning
to the compiler. They are called keywords and they cannot be used
as normal identifiers. Your C++ manual will include a complete list of these, but here are some of the more commonly used ones:
| break | case | char | class |
| const | delete | do | double |
| else | enum | for | if |
| inline | int | new | operator |
| private | protected | public | return |
| switch |
this | void | while |
G. Comments
C++ has two comment styles. First is the single line comment format,
consisting of two adjacent slashes (//). The compiler ignores any text placed after this comment format until the end of the line. Text on the following line is not affected. Thus, in
the example,
the compiler will ignore "to hold the diameter of a jelly
bean" and "int value3;" but will compile the rest
of the text. (Note that the first comment might make sense as a description of the purpose of 'value1', but the second comment makes no sense.)
The second format is used for multi-line comments. Such comments begin with a slash-asterisk pair (/*) and conclude with an asterisk-slash pair (*/). Thus, one might see the following:
/*
The code that follows will determine the volume of all
the jellybeans
in a jar (not including the air in the jar) when given the
diameter of
each jellybean. It assumes that all jellybeans have the
same basic form.
*/
Links to 'The Story of C++" and other documents
| |
Main Menu of Essentials of C++ | Next Section |