BASIC C++ DATA TYPES

advertisement
BASIC C++ DATA TYPES
As you've seen, objects are composed of two major elements: instance data
and member functions. In this lesson, I'll talk about data. More specifically, I'll
show the basic data types that are built into C++. Then I'll show member
functions in more detail.
As you'll see later, you can use C++ to define any data type you want, but the
built-in types save you from going to this trouble in most common situations.
There are seven basic types built into C++. Of these basic types, one
represents characters, three represent whole numbers (integers), and three
represent real (floating-point) numbers. Table 1-1 summarizes the C++ data
types.
Type Name
char
short
int
numbers
Used to Store
Characters
Small whole numbers
Normal-sized whole
Examples of Values Stored
'a', 'B', '$', '3', '?'
7, 30,000, -222
(same as short or same as long)
long
Large whole numbers
1,000,000,000, -123,456,789
float
Small real numbers
3.7, 199.99, -16.2, 0.000125
double
Large real numbers
7,553.393.95,47, -0.048512934
long double
Extra-large real numbers
9,123,456,789,012,345.666
Table 1-1 C++ data types
Characters
Let's look at each of these data types in turn. Characters are stored in
variables of type char. The statement
char ch3;
creates space in memory for a character and names it ch3. To store a particular
character in this variable, use a statement like
ch3 = 'a';
Character constants, such as 'a', 'B', '&' or '4', are surrounded by single
quotes.
Assignment Operator
The equal sign (=) causes the value on its right to be assigned to (placed in) the
variable on its left; that is, following this statement, the variable ch3 will have the
value 'a'. The equal sign is called the assignment operator because it assigns
the value on the right to the variable on the left.
All characters are actually stored as numbers (which, as you know, is all a
computer understands). The ASCII code is used to translate characters into
numbers. Thus 'A' is 65, 'B' is 66, and so on. The ASCII code is shown in
Appendix E.
Escape Sequences
Various special characters are represented by letters preceded by a backslash.
This is called an escape sequence because the backslash causes the
interpretation of the next character to "escape" from the normal ASCII code
and indicate something different. Table 1-2 shows the most common escape
sequences.
Escape Sequence
Character Represented
'\n'
New line. Causes the cursor to move to the start of the
next line. (Same as a carriage return plus a line feed.)
'\t'
Tab character.
'\b'
Backspace.
'\r'
Carriage return. Causes the cursor to move to the start
of this line. Also generated by the [ENTER] key.
Table 1-2 Common escape sequences
Variables of type char are occasionally used to store whole numbers rather
than characters. You can say
ch3 = 44;
However, the range of numerical values that can be stored in type char is from
-128 to 127, so this works only for very small numbers. Whole numbers are
usually stored in variables of type int, which is faster for the computer to
process than type char.
A variable of type char occupies 1 byte, or 8 bits, of memory.
Integers
Integers represent whole numbers, that is, values that can be counted, such as
the number of people living in Thomasville (12,348) or lines of text on a page
(33). Integers cannot represent numbers with decimal points or fractional parts,
such as 2.3 or 4/7. Integers can be negative: -7, -413.
There are three integer types in C++: short, int, and long. They are similar
but occupy different amounts of memory and can handle numbers in different
numerical ranges, as Table 1-3 shows. I also include type char in this table,
even though it is mostly used for characters, because it can be used to store
small whole numbers as well.
Type Name
Size
Range
char
1 byte (8 bits)
-128 to 127
short
2 bytes (16 bits)
-32,768 to 32,767
int
Same as short on 16-bit systems. Same as long on 32-bit
systems
long
4 bytes (32 bits)
-2,147,483,648 to 2,147,483,647
Table 1-3 Integer types in C++
Type short always occupies 2 bytes. It can store numbers in the range of
-32,768 to 32,767. Type long always occupies 4 bytes and can store numbers
in the range of -2,147,483,648 to 2,147,483,647.
In 16-bit systems, type int occupies 2 bytes, the same as short. In 32-bit
systems, it occupies 4 bytes, the same as long, and can therefore handle a
larger range of values. Older operating systems, such as DOS and Windows
3.1, are 16-bit systems. Newer systems, such as Windows 95, OS/2, and
Windows NT, are 32-bit systems. Unix has always been a 32-bit system.
The int type is the most commonly used integer type and operates the most
efficiently whatever system you use. However, if you want to guarantee a
2-byte variable even in a 32-bit system (to save space), you must use short; if
you want to guarantee a 4-byte variable on a 16-bit system (to hold large
values), you must use long.
Here's an example of defining some integer variables and giving them values:
int MilesDriven; <-- declare variables
long population;
MilesDriven = 1024; <-- give the values
population = 1405836L;
The L is used to designate a type long constant, one that won't fit in an integer
(in a 16-bit system).
Unsigned Integers
All the integer types have unsigned versions. Unsigned variables can't hold
negative numbers, but their range of positive values is twice as large as that of
their signed brothers. Table 1-4 shows how this looks.
Type Name
Size
Range
unsigned char
1 byte (8 bits)
0 to 255
unsigned short
2 bytes (16 bits)
0 to 65,535
unsigned int or
unsigned
Same as unsigned short on 16-bit systems. Same
as unsigned long on 32-bit systems
unsigned long
4 bytes (32 bits)
0 to 4,294,967,295
Table 1-4 Unsigned integers
Ordinary integers, without the unsigned designation, are signed by default.
You can use the keyword signed, but it's not necessary.
Floating Point
Floating-point numbers are used to represent values that can be measured, such
as the length of a room (which might be 5.32 meters) or the weight of a rock
(124.65 pounds). Floating-point values are normally expressed with a whole
number to the left of a decimal point and a fraction to the right.
Instead of a decimal point, you can use exponential notation for floating-point
numbers. Thus, 124.65 in normal notation is 1.2465e2 in exponential notation,
where the number following the e indicates the number of digits the decimal
point must be moved to the right to restore the number to normal notation.
Exponential notation is commonly used to display numbers that are
inconveniently long in decimal notation. Thus, 9,876,000,000,000,000,000 in
normal notation is 9.876e18 in exponential notation.
There are three kinds of floating-point numbers in popular operating systems, as
shown in Table 1-5. (Some systems don't have long double.)
Type Name
Size
Range
Precision
float
4 bytes (32 bits)
10e-38 to 10e38
5 digits
double
8 bytes (64 bits)
10e-308 to 10e308
15 digits
long double
10 bytes (80 bits)
10e-4932 to 10e4932
19 digits
Table 1-5 Floating-point numbers
The most common floating-point type is probably type double, which is used
for most C++ mathematical library functions. Type float requires less memory
than double and may speed up your calculations. Type long double is used
in the floating-point processor in Intel microprocessors and is useful for very
large numbers.
Here's an example of defining and using variables of the various floating-point
types:
float pi_float;
double pi_double;
long double pi_long_double;
pi_float = 3.1415;
pi_double = 3.14159265358979;
pi_long_double = 3.141592653589793238;
Here I've assigned constants representing the mathematical constant pi to
variables of the three types, using as many digits of precision as each type will
allow.
Figure 1-11 shows the amounts of memory required for all the data types
except long double.
Figure 1-11 Variables of basic data types in memory initialization
You can initialize variables to specific values when they are first declared. Thus
the six statements above could be condensed into
float pi_float = 3.1415;
double pi_double = 3.14159265358979;
long double pi_long_double = 3.141592653589793238;
Whitespace
C++ doesn't mind if you put extra spaces into a program line. You can use
them to align things so they're easier to read. You could say
float pi_float = 3.1415;
double pi_double = 3.14159265358979;
long double pi_long_double = 3.141592653589793238;
You can put as many spaces, tabs, and new lines as you want in your program.
These characters constitute whitespace, and the C++ compiler ignores
whitespace in almost all situations. Programmers use whitespace to make the
program easier for humans to follow.
Comments
You can add comments to your program listing to help yourself--and anyone
else who might need to look at your listing--understand what it does.
Comments are preceded with a double slash: //.
Here's a code fragment, taken from the previous section, that uses a full-line
comment and comments following each of three statements:
// these variables are declared and initialized
at the same time
float pi_float = 3.1415; // 5-digit precision
double pi_double = 3.14159265358979; // 15-digit
precision
long double pi_long_double = 3.141592653589793238; //
19-digit precision
Any text following the // symbol until the end of the line is ignored by the
compiler.
Another kind of comment allows you to write comments that span multiple lines.
Here's an example:
/*
if you have a really long multiline
comment it is easier to
use this arrangement than to write
the double-slash symbol before
every line
*/
The /* symbol starts the comment and the */ ends it. The end of a line does
not automatically end a comment that starts with /* (as it does with those
starting with //), so you can write as many comment lines as you want before
terminating with the */ symbol. This comment style is harder to type and
requires two symbols per comment instead of one, so it is not usually used for
single-line comments.
As you know, adding numerous comments to your code makes it far more
comprehensible to anyone else who must understand your program. And,
difficult as it may be to believe, you yourself may find comments helpful if you
try to read a listing you haven't seen for some time.
Download