Chapter 2
Introduction
to the
C++ Language
©Brooks/Cole, 2004
2.1
Background
©Brooks/Cole, 2004
2.2
C++ Programs
©Brooks/Cole, 2004
Figure 2-2Structure of a C++ program
©Brooks/Cole, 2004
Figure 2-3The greeting program
©Brooks/Cole, 2004
Figure 2-4Examples of comments
©Brooks/Cole, 2004
Figure 2-5Nested block comments are invalid
©Brooks/Cole, 2004
2.3
Identifiers
■ They allow us to
name data and other
objects in the
program.
©Brooks/Cole, 2004
Rules for giving names to
identifiers
■ Alphabet a to z , A to Z
■ Digits 0 to 9
■ No space
■ No sp ch other than _(under score)
■ Cannot start with digit
■ Is case sensitive
■ Cannot be a key word
©Brooks/Cole, 2004
2.4
Data Types
■ It defines a set of op that can be
applied on those values.
■ Set of values for each type is known
as the domain of that type
©Brooks/Cole, 2004
Figure 2-6Standard data types
■ Derived types-Pointers, arrays,structures
©Brooks/Cole, 2004
Figure 2-7Integer types
©Brooks/Cole, 2004
©Brooks/Cole, 2004
CHAR TYPE
■ Any single character from the ch set.
■ Uses one byte.
©Brooks/Cole, 2004
Note:
A character in C++ can be interpreted
as a small integer (between 0 and 255).
For this reason, C++ often treats a
character like an integer.
©Brooks/Cole, 2004
Figure 2-8Floating-point types
©Brooks/Cole, 2004
©Brooks/Cole, 2004
Note:
In C++ the Boolean constants are true
and false. Additionally, following
traditional standards, any nonzero
number is considered true, and zero is
considered false.
©Brooks/Cole, 2004
©Brooks/Cole, 2004
2.5
Variables
©Brooks/Cole, 2004
Figure 2-9Variables in memory
©Brooks/Cole, 2004
Note:
When a variable is defined, it is not
initialized. The programmer must
initialize any variable requiring
prescribed data when the function
starts.
©Brooks/Cole, 2004
Variable Initialization
■ int count=0;
■ int a,b=0;
■ int a=c=d=0;
©Brooks/Cole, 2004
2.6
Constants
©Brooks/Cole, 2004
©Brooks/Cole, 2004
The default type for floating-point literals is
double. Floating-point literals of type float
or long double can be specified by adding
one of the following suffixes:
Suffix Type
f or F float
l or L long double
1 3.14159L // long double
2 6.02e23f // float
©Brooks/Cole, 2004
Note:
A character constant is enclosed in
single quotes.
©Brooks/Cole, 2004
Note:
Use single quotes for character
constants.
Use double quotes for string constants.
©Brooks/Cole, 2004
Note:
The only bool types constants are true,
printed as 1, and false, printed as 0.
©Brooks/Cole, 2004
Other literals
Three keyword literals exist in C++: true, false and nullptr:
true and false are the two possible values for variables of type bool.
nullptr is the null pointer value.
bool foo = true;
bool bar = false;
int* p = nullptr;
©Brooks/Cole, 2004
Figure 2-11 Null characters and null strings
'\0' Null character
"" Empty string
©Brooks/Cole, 2004
2.7
Coding Constants
©Brooks/Cole, 2004
■ literal constant---unnamed constant
‘a’, 5, “hello”
Most common form of constant
■ Defined constant
#define tax_rate 10
■ Memory constant
Const float pi=3.141
const char tab = '\t';
©Brooks/Cole, 2004
Input statements
cin >> variable-name;
Meaning: read the value of the variable called
<variable-name> from the user
Example:
cin >> a;
cin >> b >> c;
cin >> x;
cin >> my-character;
Output statements
cout << variable-name;
Meaning: print the value of variable <variable-name> to the user
cout << “any message “;
Meaning: print the message within quotes to the user
cout << endl;
Meaning: print a new line
Example:
cout << a;
cout << b << c;
cout << “This is my character: “ << my-character
<< “ he he he”
<< endl;