You are on page 1of 41

Week 1: Introduction to C++ Programming

Starting Out with C++


Early Objects
Seventh Edition

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley


Important Information

Assessment methods

1. Coursework – 50%
Practical Test (15%) - week 8 (Saturday)
Test 1 (10%) - week 7
Test 2 (10%) - week 10
Group assignment (15%) - week 12

2. Final Exam – 50%

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-2


1-2
Recommended Text books:

Gaddis T, (2015). Starting Out with C++: From Control


Structures to Objects (8th ed.). Pearson/ Prentice Hall.

D.S Malik (2015) C++ Programming: From Problem


Analysis to Program Design (7th ed.). Cengage Learning

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-3


Lecturer and Tutor Info.

Lecturer: Mr. Steven Phan Koo Yuen


Room: N-G037
Email: phanky@utar.edu.my , 0192348826

Tutor: Mr. Steven Phan Koo Yuen


Email: : phanky@utar.edu.my

Tutor: Dr. Tan Joi San


Email: : jstan@utar.edu.my

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-4


Advise and Rules
• Study the lecture note in advance.
• Attempt the practical questions in advance at
home.
• Buy the Text Book.
• Spend 2 hours every day to practice the
programming.
• Consult the lecturer ASAP if you are not
understand.
• Show your effort in attempting the questions
before asking the solutions.
• Get Ready to answers any questions in the
class.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-5
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-6
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-7


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-8


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-9


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-10


From a High-level Program to an
Executable File

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-11


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-12


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-13
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-14


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-15
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-16


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-17


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-18


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-19


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-20


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-21


Basic flowcharting symbols

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-22


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-23


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-24


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-26


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-27


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-28


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-29


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-30


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-31


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-32


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-33


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-34


2-34
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-35


2-35
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-36


2-36
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-37


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-38


2-38
// 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-39


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-40


2-40
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-41

You might also like