|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Home : Math & Science : Computer Science Study Guides : C++ Fundamentals : Getting Started : Data Types
Data Types
Programming is essentially the manipulation of many variables. The basic
variable data types which C++ variables can assume are: int (integers),
char (characters) and doubles (real decimals). Declaring a variable
simply means letting the computer know that you want to reserve a spot in memory
for it. Here is an example of declaring a variable:
int number_of_items; Defining (or initializing) a variable is giving it a value. Variables can be
defined at their
declaration:
char initial = 'B'; double x_coordinate = 46.72168000517; Of course, variables can be assigned values at a later time, too:
int trees, shrubs; trees = 6; shrubs = trees;
It is important to note that characters are represented by numbers in the
computer, and a char variable can be assigned a numerical value.
Characters, Escape Sequences, and Strings
Some characters do not have their own symbol and must be referenced by an
escape sequence. Some escape sequences are:
'\\' -- backslash
'\'' -- single quotation mark
'\"' -- double quotation mark
'\b' -- backspace
'\n' -- new line
'\r' -- carriage return
'\t' -- tab
More Data Types
There are more primitive data types in C++ which are variation on the ones
described above. They are as follows:
Constant Variables and Enumerated Types
You will often want to have a variable whose value can not be changed. For
instance, it is generally considered stylistically poor to have constants
without explanation, as in the following:
float area = 3.1415 * radius * radius;
Not only can the introduction of 3.1415 be confusing, but it is like that
you will want to use the same value elsewhere in your program. It is better to
assign the value to a constant variable using const:
const PI = 3.1415; float area = PI * radius * radius;This syntax replaces C's #define syntax for defining constants. One advantage of this is that constant values have types in C++ and can therefore be checked at compile time.
Sometimes you may want to forget entirely that your variables are being
represented by numbers. In keeping track of the days of the week, you might
like to write something like:
const int SUNDAY = 0; const int MONDAY = 1; const int TUESDAY = 2; etc.
This will work, but C++ lets you more easily create your own enumerated type
as follows:
enum day_type {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
day_type favorite_day;
favorite_day = Saturday;
The enum command lets you define a new data type and the (constant)
values a variable of this new data type can take on. It lets you do this all in
one step and hides the fact that your values are being represented by numbers.
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contact Us | Privacy Policy | Terms and Conditions | About
©2006 SparkNotes LLC, All Rights Reserved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||