Week 1: Introduction to C++ Programming
Starting Out with C++
Early Objects
Eighth Edition
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Recommended Text books:
Gaddis (2014) Starting Out with C++ From Control
Structures Through Objects. (8th ed.). Pearson
OR
Gaddis (2014) Starting Out with C++ Early Objects. (8th
ed.). Pearson
D.S Malik (2014) C++ Programming: From Problem
Analysis to Program Design (7th ed.). Cengage Learning
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-2
Topics
1.1 Programs and Programming Languages
1.2 What Is a Program Made of?
1.3 Input, Processing, and Output
1.4 The Programming Process
1.5 Structured Programming
1.6 C++ Program
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
1-3
1.1 Programs and Programming
Languages
• Program
a set of instructions directing a computer to
perform a task
• Programming Language
a language used to write programs
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-4
Programs and Programming Languages
Types of languages
– Low-level: used for communication with
computer hardware directly.
– High-level: closer to human language
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-5
From a High-level Program to an
Executable File
a) Create file containing the program with a
text editor.
b) Run preprocessor to convert source file
directives to source code program
statements.
c) Run compiler to convert source program
statements into machine instructions.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-6
From a High-level Program to an
Executable File
d) Run linker to connect hardware-specific
library code to machine instructions,
producing an executable file.
Steps b) through d) are often performed by a
single command or button click.
Errors occuring at any step will prevent
execution of the following steps.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-7
From a High-level Program to an
Executable File
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-8
1.2 What Is a Program Made Of?
Common elements in programming
languages:
– Key Words
– Programmer-Defined Identifiers
– Operators
– Punctuation
– Syntax
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-9
Example Program
#include <iostream>
using namespace std;
int main()
{
double num1 = 5,
num2, sum;
num2 = 12.5;
sum = num1 + num2;
cout << "The sum is " << sum;
system(“pause”);
return 0;
}
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-10
Lines vs. Statements
In a source file,
A line is all of the characters entered before a
carriage return.
Blank lines improve the readability of a
program.
Here are four sample lines. Line 3 is blank:
double num1 = 5, num2, sum;
num2 = 12;
sum = num1 + num2;
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-11
Lines vs. Statements
In a source file,
A statement is an instruction to the computer to
perform an action.
A statement may contain keywords, operators,
programmer-defined identifiers, and
punctuation.
A statement may fit on one line, or it may
occupy multiple lines.
Here is a single statement that uses two lines:
double num1 = 5,
num2, sum;
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-12
Variables
• A variable is a named location in computer
memory (in RAM)
• It holds a piece of data
• It must be defined before it can be used
• Example variable definition:
- double num1;
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-13
1.3 Input, Processing, and Output
Three steps that many programs perform
1) Gather input data
- from keyboard
- from files on disk drives
2) Process the input data
3) Display the results as output
- send it to the screen or a printer
- write it to a file
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-14
1.4 The Programming Process
1. Define what the program is to do.
2. Visualize the program running on the
computer.
3. Use design tools to create a model of the
program.
Hierarchy charts, flowcharts, pseudocode, etc.
4. Check the model for logical errors.
5. Write the program source code.
6. Compile the source code.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-15
The Programming Process (cont.)
7. Correct any errors found during compilation.
8. Link the program to create an executable file.
9. Run the program using test data for input.
10. Correct any errors found while running the
program.
Repeat steps 4 - 10 as many times as necessary.
11. Validate the results of the program.
Does the program do what was defined in step 1?
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-16
1.5 Structured Programming
• A method for designing and coding
programs in a systematic, organized
manner
• It combines the principles of top-down
design, modularity and the use of the three
accepted control structures: sequence,
repetition and selection
• Sequence, repetition and selection can be
expressed in pseudocode, or with
flowcharts
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-17
Flowcharts START
Display message “How
• A flowchart is a diagram
many hours did you
work?”
that depicts the “flow” of a
Read Hours
program.
• The figure shown here is a
Display message “How
much do you get paid per
hour?”
flowchart for the pay-
calculating program Read Pay Rate
Multiply Hours by Pay
Rate. Store result in
Gross Pay.
Display Gross Pay
END
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-18
Basic flowcharting symbols
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-19
Basic Flowchart START
Terminal
Symbols Display message “How
many hours did you
work?”
• Terminals Read Hours
– represented by rounded Display message “How
much do you get paid per
rectangles hour?”
– indicate a starting or
ending point Read Pay Rate
Multiply Hours by Pay
Rate. Store result in
START Gross Pay.
END Display Gross Pay
Terminal
END
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-20
START
Basic Flowchart Display message “How
Symbols many hours did you
work?”
• Input/Output Operations Read Hours
– represented by
parallelograms Display message “How
much do you get paid per
hour?” Input/Output
– indicate an input or output
Operation
operation
Read Pay Rate
Multiply Hours by Pay
Rate. Store result in
Gross Pay.
Display message “How
many hours did you
work?” Read Hours
Display Gross Pay
END
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-21
Basic Flowchart START
Symbols Display message
“How many
hours did you
• Processes work?”
– represented by rectangles Read Hours
– indicates a process such as Display message
a mathematical computation “How much do
you get paid per
or variable assignment hour?”
Read Pay Rate
Multiply Hours Multiply Hours
by Pay Rate. Process
by Pay Rate.
Store result in
Store result in Gross Pay.
Gross Pay.
Display Gross
Pay
END
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
START
Stepping Through the Display message “How
many hours did you
Flowchart work?”
Read Hours
Your gross pay is
800
Display message “How
much do you get paid per
hour?”
Read Pay Rate
Multiply Hours by Pay
Rate. Store result in
Gross Pay.
Variable Contents:
Hours: 40
Pay Rate: 20 Output Operation Display Gross Pay
Gross Pay: 800
END
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-23
Control Structures
In 1960s computer scientists proved there are only 3 basic control
structures (also called constructs) needed to create any program or
algorithm!
• Sequence –in sequential order.
– The simplest of control structures – start at the beginning and
continue in sequential order.
• Selection – selectively execute statements
– Also called a branch or decision
– requires a condition to determine when to execute statements.
• Repetition – repeat statements more than once
– Also called a loop
– needs a stop condition, i.e, the program will continue to loop
until some condition is met.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-24
Sequence Structure
• a series of actions are performed in sequence
• The pay-calculating example was a sequence
flowchart.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-25
Decision Structure
• In the flowchart segment below, the question “is
marks > 50?” is asked. If the answer is no, then
process Fail is performed. If the answer is yes, then
process Pass is performed.
NO YES
Marks > 50?
Process Fail Process Pass
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-26
Repetition Structure
• In the flowchart segment, the question “is x < y?” is
asked. If the answer is yes, then Process A is
performed. The question “is x < y?” is asked again.
Process A is repeated as long as x is less than y.
When x is no longer less than y, the repetition stops
and the structure is exited.
YES
x < y? Process A
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-27
Controlling a Repetition Structure
• The action performed by a repetition structure must
eventually cause the loop to terminate. Otherwise, an
infinite loop is created.
• In this flowchart segment, x is never changed. Once
the loop starts, it will never end.
• QUESTION: How can this
flowchart be modified so
it is no longer an infinite YES
loop? x < y? Display x
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-28
Case Structure
• One of several possible actions is taken,
depending on the contents of a variable. (the
value of Years_employed)
If years_employed = 2, bonus is set If years_employed = 3, bonus is set
to 200 to 400
If years_employed = 1, bonus is set If years_employed is any other
CASE value, bonus is set to 800
to 100 years_employed
1 2 3 Other
bonus = 100 bonus = 200 bonus = 400 bonus = 800
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-29
Modules
• A program module (such START
as a function in C++) is
represented by a special Read Input.
symbol.
Call calc_pay
function.
Display results.
END
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-30
1.6 The Parts of a C++ Program
// sample C++ program comment
#include <iostream> preprocessor directive
using namespace std; which namespace to use
int main() beginning of function named main
{ beginning of block for main
cout << "Hello, there!"; output statement
return 0; send 0 back to operating system
} end of block for main
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-31
2-31
The Parts of a C++ Program
Statement Purpose
// sample C++ program comment
#include <iostream> preprocessor directive
using namespace std; which namespace to use
int main() beginning of function named main
{ beginning of block for main
cout << "Hello, there!"; Output statement
system(“pause”); Pause your program b4 disappear
return 0; send 0 back to the operating system
} end of block for main
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-32
2-32
Special Characters
Character Name Description
// Double Slash Begins a comment
# Pound Sign Begins preprocessor directive
< > Open, Close Brackets Encloses filename used in
#include directive
( ) Open, Close Parentheses Used when naming function
{ } Open, Close Braces Encloses a group of statements
" " Open, Close Quote Marks Encloses string of characters
; Semicolon Ends a programming statement
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-33
2-33
Important Details
• C++ is case-sensitive. Uppercase and
lowercase characters are different
characters. ‘Main’ is not the same as
‘main’.
• Every { must have a corresponding }, and
vice-versa.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-34
The cout Object
• Displays information on computer screen
• Use << to send information to cout
cout << "Hello, there!";
• Can use << to send multiple items to cout
cout << "Hello, " << "there!";
Or
cout << "Hello, ";
cout << "there!";
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-35
2-35
// A simple C++ program
#include <iostream>
using namespace std;
int main()
{
cout << "Programming is great fun!";
cout << "Programming is " << "great fun!";
cout << "Programming is ";
cout << "great fun!";
system(“pause”);
return 0;
}
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-36
Starting a New Line
• To get multiple lines of output on screen
- Use endl
cout << "Hello, there!" << endl;
cout << "Programming is great fun!“ <<
endl << endl;
- Use \n in an output string
cout << "Hello, there!\n";
cout << "Programming is great
fun!\n\n“;
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-37
2-37
The #include Directive
• Inserts the contents of another file into the
program
• Is a preprocessor directive
– Cout is not part of the C++ language No ; goes here
• Example:
#include <iostream>
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-38