Professional Documents
Culture Documents
Introduction To C++: For Educational Purpose Only. Not To Be Circulated Without This Banner
Introduction To C++: For Educational Purpose Only. Not To Be Circulated Without This Banner
Introduction To C++: For Educational Purpose Only. Not To Be Circulated Without This Banner
Introduction to C++
Program 2-1
//A simple C++ program
#include <iostream.h>
For Educational
Only. Not to be circulated without this banner.
void Purpose
main (void)
{
cout<< Programming is great fun!;
}
Program Output:
Programming is great fun!
3
For
Table 2-1
Character
//
#
<>
Name
double slash
Pound sign
Opening and
closing
brackets
()
Opening and
Educational Purpose
Only.
closing
parenthesis
{}
Opening and
closing braces
""
Opening and
closing
quotation
marks
;
Semicolon
Description
Marks the beginning of a comment.
Marks the beginning of a preprocessor
directive
Encloses a filename when used with the
#include directive
Used in naming a function, as in
Not
to be()circulated
int main
output object.
Its job is to output information using the
standard output device
Program 2-2
// A simple C++ program
#include <iostream.h>
void main (void)
For Educational
Purpose Only. Not to be circulated without this banner.
{
cout<< Programming is << great fun!;
}
Output:
Programming is great fun!
6
Program 2-3
// A simple C++ program
#include <iostream.h>
void main (void)
{
For Educational Purpose Only. Not to be circulated without this banner.
cout<< Programming is ;
cout << great fun!;
}
Output:
Programming is great fun!
7
For
Program 2-4
// An unruly printing program
#include <iostream.h>
void main(void)
{
cout << "The following items were top sellers";
cout << "during the month of June:";
cout << "Computer
games";
Educational
Purpose Only.
Not to be circulated without
cout << "Coffee";
cout << "Aspirin";
}
this banner.
Program Output
The following items were top sellersduring the month of
June:Computer gamesCoffeeAspirin
8
New lines
cout does not produce a newline at the end
of a statement
To produce
a Only.
newline,
either the
stream
For Educational
Purpose
Not to use
be circulated
without
this banner.
manipulator endl
or the escape sequence \n
For
Program 2-5
// A well-adjusted printing program
#include <iostream.h>
void main(void)
{
cout << "The following items were top sellers" << endl;
cout << "during the month of June:" << endl;
Educational
Purpose Only.
to be circulated without this
cout << "Computer
games"Not
<< endl;
cout << "Coffee" << endl;
cout << "Aspirin" << endl;
}
banner.
10
Program Output
The following items were top sellers
during the month of June:
Computer games
For Educational
Purpose Only. Not to be circulated without this banner.
Coffee
Aspirin
11
Program 2-6
// Another well-adjusted printing program
#include <iostream.h>
void main(void)
For Educational Purpose Only. Not to be circulated without this banner.
{
cout << "The following items were top sellers" << endl;
cout << "during the month of June:" << endl;
cout << "Computer games" << endl << "Coffee";
cout << endl << "Aspirin" << endl;
}
12
Program Output
The following items were top sellers
during the month of June:
Computer games
For Educational
Purpose Only. Not to be circulated without this banner.
Coffee
Aspirin
13
Program 2-7
// Yet another well-adjusted printing program
#include <iostream.h>
using namespace std;
Forvoid
Educational
Purpose Only. Not to be circulated without this banner.
main(void)
{
cout
cout
cout
cout
<<
<<
<<
<<
14
Program Output
The following items were top sellers
during the month of June:
Computer games
For Educational
Purpose Only. Not to be circulated without this banner.
Coffee
Aspirin
15
For
Table 2-2
Table 2-2 Common Escape Sequences
Escape
Name
Description
Sequence
\n
Newline
Causes the cursor to go to the
next line for subsequent printing
\t
Horizontal
Causes the cursor to skip over to
tab
the next tab stop
\a
Alarm
Causes the computer to beep
\b
Backspace
the be
cursor
to back up, orwithout
Educational
Purpose
Only.Causes
Not to
circulated
move left one position
\r
Return
Causes the cursor to go to the
beginning of the current line, not
the next line.
\\
Backslash
Causes a backslash to be printed
\'
Single quote Causes a single quotation mark
to be printed
\"
Double quote Causes a double quotation mark
to be printed
this banner.
16
17
18
For
Program 2-8
#include <iostream.h>
void main(void)
{
int value;
value = 5;
Educational
Only.
be circulated
cout << Purpose
The value
is Not
<<tovalue
<< endl;without
}
this banner.
Program Output:
The value is 5
19
Assignment statements:
Value = 5;
Constants
A variable is called a variable because its value
may be changed. A constant, on the other hand, is
a data item whose value does not change during
For Educational
Purposeexecution.
Only. Not to be circulated without this banner.
the programs
21
For
Program 2-10
#include <iostream.h>
void main (void)
{
Educational
Purpose Only. Not to be circulated without this banner.
int apples;
apples = 20;
cout<< Today we sold << apples << bushels\n;
cout << of apples.\n;
}
22
Program Output
23
24
25
For
a u to
char
d e f a u lt
e ls e
f lo a t
in li n e
new
r e g is t e r
Purpose
s iz e o f
t e m p la t e
ty p e d e f
u s in g
w h ile
b re a k
c la s s
d e le t e
enum
fo r
in t
o p e ra to r
r e i n Not
t e r p r e t to
_ c a sbe
t
Only.
s t a t ic
t h is
t y p e id
v ir t u a l
bool
const
do
e x p li c i t
f r ie n d
lo n g
p r iv a t e
re tu rn
circulated
without
s t a t ic _ c a s t
th r o w
ty p e n a m e
v o id
case
c o n s t_ c a s t
d o u b le
e x te rn
g o to
m u t a b le
p ro te c te d
sthis
h o r t banner.
s tru c t
tr u e
u n io n
v o la t il e
26
Legal or Illegal?
dayOfWeek
Legal
3dGraph
begin with without
a digit. this banner.
For Educational
Purpose Only.Illegal.
Not toCannot
be circulated
_employee_num
Legal
june1997
Legal
Mixture#3
27
28
Table 2-5
Table 2-5 Integer Data Types, Sizes and Ranges
Data Type
Size
Range
short
2 Bytes -32,768 to +32.767
unsigned short
2 Bytes 0 to +65,535
int
4 Bytes
-2,147,4833,648
to +2,147,4833,647
For Educational
Purpose
Only. Not
to be circulated
without this banner.
unsigned int
4 Bytes 0 to 4,294,967,295
long
4 Bytes -2,147,4833,648 to +2,147,4833,647
unsigned long
4 Bytes 0 to 4,294,967,295
29
For
Program 2-11
// This program has variables of several of the integer types.
#include <iostream.h>
void main(void)
{
int checking;
unsigned int miles;
long days;
Educational
Only. Not to be circulated without this
checking Purpose
= -20;
miles = 4276;
days = 184086;
cout << "We have made a long journey of " << miles;
cout << " miles.\n";
cout << "Our checking account balance is " << checking;
cout << "\nExactly " << days << " days ago Columbus ";
cout << "stood on this spot.\n";
}
banner.
30
Program Output
We have made a long journey of 4276 miles.
Our checking account balance is -20
Exactly 184086 days ago Columbus stood on this spot.
31
Program 2-12
// This program shows three variables declared on the same
// line.
#include <iostream.h>
void main(void)
{
int floors,rooms,suites;
32
Program Output
The Grande Hotel has 15 floors
with 300 rooms and 30 suites.
33
34
Program 2-13
// This program demonstrates the close relationship between
// characters and integers.
#include <iostream.h>
void main(void)
For Educational
Purpose Only. Not to be circulated without this banner.
{
char letter;
letter = 65;
cout << letter << endl;
letter = 66;
cout << letter << endl;
}
36
Program Output
A
B
For Educational Purpose Only. Not to be circulated without this banner.
37
For
Program 2-14
// This program uses character constants
#include <iostream.h>
void main(void)
Educational
Purpose
{
char letter;
letter = 'A';
cout << letter << endl;
letter = 'B';
cout << letter << endl;
}
38
Program Output
A
B
For Educational Purpose Only. Not to be circulated without this banner.
39
C-Strings
C-Strings are consecutive sequences of
characters and can occupy several bytes of
memory.
For Educational
Purpose Only. Not to be circulated without this banner.
C-Strings always have a null terminator at
the end. This marks the end of the string.
Escape sequences are always stored
internally as a single character.
40
Program 2-15
// This program uses character constants
#include <iostream.h>
void main(void)
{
char letter;
letter = 'A';
For Educational
Purpose Only. Not to be circulated without this banner.
cout << letter << '\n';
letter = 'B';
cout << letter << '\n';
41
Program Output
A
B
For Educational Purpose Only. Not to be circulated without this banner.
42
43
44
45
Table 2-7
Table 2-7 Floating Point Data Types on PCs
Data Type
Key Word
Description
Single Precision
float
Double
long double
8 bytes.Only.
NumbersNot
between
ForLong
Educational
Purpose
to be circulated without this banner.
Precision
46
For
Program 2-17
// This program uses floating point data types
#include <iostream.h>
void main(void)
{
Educational
Purpose Only. Not to be circulated without this banner.
float distance;
double mass;
distance = 1.495979E11;
mass = 1.989E30;
cout << "The Sun is " << distance << " kilometers away.\n";
cout << "The Sun\'s mass is " << mass << " kilograms.\n";
}
47
Program Output
The Sun is 1.4959e+11 kilometers away.
The Sun's mass is 1.989e+30 kilograms.
For Educational Purpose Only. Not to be circulated without this banner.
48
49
50
For
Program 2-18
#include <iostream.h>
void main (void)
{
bool boolValue;
boolValue
= true;Only. Not to
Educational
Purpose
cout << boolValue << endl;
boolValue = false;
cout << boolValue << endl;
}
51
Program Output
1
0
For Educational
Purpose
Only. Notastothe
benumber
circulated
without
this banner.
Internally, true
is represented
1 and
false is
52
53
Program 2-19
#include <iostream.h>
void main (void)
{
long double apple;
cout << The size of an integer is << sizeof(int);
cout << bytes.\n;
cout << The size of a long integer is << sizeof(long);
cout << bytes.\n;
cout << An apple can be eaten in << sizeof(apple);
cout << bytes!\n;
}
54
Program Output
The size of an integer is 4 bytes.
The size of a long integer is 4 bytes.
An apple can be eaten in 8 bytes!
For Educational Purpose Only. Not to be circulated without this banner.
55
56
Program 2-20
#include <iostream.h>
void main (void)
{
int month = 2, days = 28;
cout << Month << month << has << days << days.\n;
For}Educational Purpose Only. Not to be circulated without this banner.
Program output:
Month 2 has 28 days.
57
58
Program 2-21
// This program can't find its variable
#include <iostream.h>
For Educational
Purpose Only. Not to be circulated without this banner.
void main(void)
{
cout << Value;
int Value = 100;
}
59
Table 2-8
Table 2-8 Fundamental Arithmetic Operaotrs
Operator Meaning
Type
Example
+
Addition
Binary
Total = Cost + Tax;
Subtraction
Binary
Cost = Total - Tax;
*
Multiplication
= Cost *without
Rate; this banner.
For Educational
Purpose Only.Binary
Not to be Tax
circulated
/
Division
Binary
SalePrice = Original/2;
%
Modulus
Binary
Remainder = Value%3;
61
Program 2-22
// This program calculates hourly wages
// The variables in function main are used as follows:
// regWages: holds the calculated regular wages.
// basePay: holds the base pay rate.
// regHours: holds the number of hours worked less overtime.
// otWages: holds the calculated overtime wages.
// otPay: holds the payrate for overtime hours.
// otHours: holds the number of overtime hours worked.
// totalWages: holds the total wages.
For Educational
Purpose Only. Not to be circulated without this banner.
#include <iostream.h>
void main(void)
{
float regWages, basePay = 18.25, regHours = 40.0;
float otWages, otPay = 27.78, otHours = 10;
float totalWages;
regWages = basePay * regHours;
otWages = otPay * otHours;
totalWages = regWages + otWages;
cout << "Wages for this week are $" << totalWages << endl;
}
62
2.16 Comments
Comments are notes of explanation that document lines or
sections of a program.
Comments are part of a program, but the compiler ignores
them. They
are intended
fortopeople
who maywithout
be reading
For Educational
Purpose
Only. Not
be circulated
this banner.
the source code.
Commenting the C++ Way
//
Commenting the C Way
/*
*/
63
Program 2-23
//
//
//
//
PROGRAM: PAYROLL.CPP
Written by Herbert Dorfmann
This program calculates company payroll
Last modification: 3/30/96
#include <iostream.h>
For Educational
Purpose Only. Not to be circulated without this banner.
void main(void)
{
float payRate;
float hours;
int empNum;
64
Program 2-24
//
//
//
//
//
//
//
//
PROGRAM: PAYROLL.CPP
Written by Herbert Dorfmann
Also known as "The Dorfmiester"
Last modification: 3/30/96
This program calculates company payroll.
Payroll should be done every Friday no later than
12:00 pm. To start the program type PAYROLL and
press the enter key.
#include <iostream.h>
//
//
//
//
//
//
//
//
//
//
65
Program 2-25
/*
PROGRAM: PAYROLL.CPP
Written by Herbert Dorfmann
This program calculates company payroll
Last modification: 3/30/96
*/
For Educational
Purpose Only. Not to be circulated without this banner.
#include <iostream.h>
void main(void)
{
float payRate;
float hours;
int empNum;
66
Program 2-26
/*
PROGRAM: PAYROLL.CPP
Written by Herbert Dorfmann
This program calculates company payroll
Last modification: 3/30/96
*/
#include <iostream.h>
67
68
Program 2-27
#include <iostream.h>
void main(void){float shares=220.0;float avgPrice=14.67;cout
<<"There were "<<shares<<" shares sold at $"<<avgPrice<<"
per share.\n";}
Program Output
For Educational
Purpose Only. Not to be circulated without this banner.
There were 220 shares sold at $14.67 per share.
69
For
Program 2-28
// This example is much more readable than Program 2-26.
#include <iostream.h>
void main(void)
{
float shares = 220.0;
float avgPrice
= 14.67;
Educational
Purpose
Only.
cout << "There were " << shares << " shares sold at $";
cout << avgPrice << " per share.\n";
}
Program Output
There were 220.0 shares sold at $14.67 per share.
70