Introduction To C++
Introduction To C++
Introduction to
C++
2.1
The Part of a C++ Program
Programming
• A program is a set of instructions in proper sequence,
that causes a computer to perform a particular task.
• When learning to program in any programming
language, it’s best just to learn the “rules of the game.”
•Modern programs are projects composed of many of
individual program modules that have to be linked
together in order to be run. In fact most developer
systems have their own integrated development
environment (ide) and programs are developed in
phases within the ide.
Programming is
fun!
The endl Manipulator
• You do NOT put quotation marks around
endl
Programming is
fun!
2.3
The #include Directive
The #include Directive
• Inserts the contents of another file into the
program
• This is a preprocessor directive, not part of
C++ language
• #include lines not seen by compiler
• Do not place a semicolon at end of
#include line
2.4
Variables and Literals
Variables and Literals
• Variable: a storage location in memory
int item;
Variable Definition in Program 2-7
Variable Definition
Literals
• Literal: a value that is written into a
program’s code.
20 is an integer literal
String Literals in Program 2-9
itemsOrdered
totalSales
total_Sales
total.Sales
4thQtrSales
totalSale$
2.6
Integer Data Types
Integer Data Types
• Integer variables can hold whole numbers such
as 12, 7, and -99.
Defining Variables
• Variables of the same type can be defined
- On separate lines:
int length;
int width;
unsigned int area;
- On the same line:
int length, width;
unsigned int area;
• Variables of different types must be in different
definitions
Integer Types in Program 2-10
itemsOrdered = 15;
Integer Literals
Integer Literals
• Integer literals are stored in memory as
ints by default
• To store an integer constant in a long
memory location, put ‘L’ at the end of the
number: 1234L
• Constants that begin with ‘0’ (zero) are
base 8: 075
• Constants that begin with ‘0x’ are base
16: 0x75A
2.7
The char Data Type
The char Data Type
• Used to hold characters or very small
integer values
• Usually 1 byte of memory
• Numeric value of character from the
character set is stored in memory:
CODE: MEMORY:
char letter; letter
letter = 'C';
67
Character Literals
• Character literals must be enclosed in
single quote marks. Example:
'A'
Character Literals in Program 2-13
Character Strings
• A series of characters in consecutive memory
locations:
"Hello"
• Stored with the null terminator, \0, at the end:
H e l l o \0
2.8
The C++ string Class
The C++ string Class
• Special data type supports working with strings
• #include <string>
• Can define string variables in programs:
string firstName, lastName;
• Can receive values with assignment operator:
firstName = "George";
lastName = "Washington";
• Can be displayed via cout
cout << firstName << " " << lastName;
The string class in Program 2-15
2.9
Floating-Point Data Types
Floating-Point Data Types
• The floating-point data types are:
float
double
long double
1 0
bool finished = false;
Boolean Variables in Program 2-17
2.11
Determining the Size of a Data
Type
Determining the Size of a Data
Type
The sizeof operator gives the size of any
data type or variable:
double amount;
cout << "A double is stored in "
<< sizeof(double) << "bytes\n";
cout << "Variable amount is stored
in “ << sizeof(amount) << "bytes\n";
2.12
Variable Assignments and
Initialization
Variable Assignments and
Initialization
• An assignment statement uses the =
operator to store a value in a variable.
item = 12;
// ERROR!
12 = item;
Variable Initialization
• To initialize a variable means to assign it a
value when it is defined:
- subtraction ans = 7 - 3; 4
* multiplication ans = 7 * 3; 21
/ division ans = 7 / 3; 2
% modulus ans = 7 % 3; 1
Arithmetic Operators in Program 2-
21
A Closer Look at the / Operator
• / (division) operator performs integer
division if both operands are integers
cout << 13 / 5; // displays 2
cout << 91 / 7; // displays 13
• If either operand is floating point, the result
is floating point
cout << 13 / 5.0; // displays 2.6
cout << 91.0 / 7; // displays 13.0
A Closer Look at the % Operator
• % (modulus) operator computes the
remainder resulting from integer division
cout << 13 % 5; // displays 3
• % requires integers for both operands
cout << 13 % 5.0; // error
2.15
Comments
Comments
• Used to document parts of the program
• Intended for persons reading the source
code of the program:
– Indicate the purpose of the program
– Describe the use of variables
– Explain complex sections of code
• Are ignored by the compiler
Single-Line Comments
Begin with // through to the end of line:
int length = 12; // length in inches
int width = 15; // width in inches
int area; // calculated area