Program Design and Software Tools: Introduction To C++ Programming
Program Design and Software Tools: Introduction To C++ Programming
Program Design and Software Tools: Introduction To C++ Programming
IS 0020
Program Design and Software Tools
Introduction to C++ Programming
Lecture 1
Jan 6, 2004
• Lecture:
– James B D Joshi
– Tuesdays: 6:00-8.50 PM
• One (two) 15 (10) minutes break(s)
– Office Hours: Wed 3:00-5:00PM/Appointment
• Pre-requisite
– IS 0015 Data Structures and Programming Techniques
• Textbook
– C++ How to Program- Fourth Edition, by H. M. Deitel, P.
J. Deitel, Prentice Hall, New Jersey, 2003, ISBN: 0-13-
038474.
• Course Description
– An introduction to the development of
programs using C++.
– Emphasis is given to the development of
program modules that can function
independently.
• Object-oriented design
– The theory of data structures and
programming language design is continued.
• Machine language
• Only language computer directly understands
• Defined by hardware design
– Machine-dependent
• Generally consist of strings of numbers
– Ultimately 0s and 1s
• Instruct computers to perform elementary operations
– One at a time
• Cumbersome for humans
• Example:
+1300042774
+1400593419
+1200274027
• Assembly language
• English-like abbreviations representing elementary computer
operations
• Clearer to humans
• Incomprehensible to computers
– Translator programs (assemblers)
• Convert to machine language
• Example:
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY
• High-level languages
• Similar to everyday English, use common mathematical
notations
• Single statements accomplish substantial tasks
– Assembly language requires many instructions to
accomplish simple tasks
• Translator programs (compilers)
– Convert to machine language
• Interpreter programs
– Directly execute high-level language programs
• Example:
grossPay = basePay + overTimePay
• History of C
– Evolved from two other programming languages
• BCPL and B
– “Typeless” languages
– Dennis Ritchie (Bell Laboratories)
• Added data typing, other features
– Development language of UNIX
– Hardware independent
• Portable programs
– 1989: ANSI standard
– 1990: ANSI and ISO standard published
• ANSI/ISO 9899: 1990
• History of C++
– Extension of C
– Early 1980s: Bjarne Stroustrup (Bell Laboratories)
– Provides capabilities for object-oriented programming
• Objects: reusable software components
– Model items in real world
• Object-oriented programs
– Easy to understand, correct and modify
– Hybrid language
• C-like style
• Object-oriented style
• Both
• C++ programs
– Built from pieces called classes and functions
• C++ standard library
– Rich collections of existing classes and functions
• “Building block approach” to creating programs
– “Software reuse”
• Java
– 1991: Sun Microsystems
• Green project
– 1995: Sun Microsystems
• Formally announced Java at trade show
– Web pages with dynamic and interactive content
– Develop large-scale enterprise applications
– Enhance functionality of web servers
– Provide applications for consumer devices
• Cell phones, pagers, personal digital assistants, …
• Objects
– Reusable software components that model real world items
– Meaningful software units
• Date objects, time objects, paycheck objects, invoice objects, audio
objects, video objects, file objects, record objects, etc.
• Any noun can be represented as an object
– More understandable, better organized and easier to maintain than
procedural programming
– Favor modularity
• Software reuse
– Libraries
• MFC (Microsoft Foundation Classes)
• Rogue Wave
• C++ systems
– Program-development environment
– Language
– C++ Standard Library
• C++ program names extensions
– .cpp
– .cxx
– .cc
– .C
6. Execute Loader puts program
in memory.
Disk ..
..
..
Primary
Memory
CPU
CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
..
..
..
executes.
2003 Prentice Hall, Inc. All rights reserved.
18
Basics of a Typical C++ Environment
• Variable names
– Correspond to actual locations in computer's memory
– Every variable has name, type, size and value
– When new value placed into variable, overwrites previous
value
• Arithmetic calculations
– * : Multiplication
– / : Division
• Integer division truncates remainder
– 7 / 5 evaluates to 1
– % : Modulus operator returns remainder
– 7 % 5 evaluates to 2
Operator(s) Operation(s) Order of evaluation (precedence)
• Computing problems
– Solved by executing a series of actions in a specific order
• Algorithm a procedure determining
– Actions to be executed
– Order to be executed
– Example: recipe
• Program control
– Specifies the order in which statements are executed
• Pseudocode
– Artificial, informal language used to develop algorithms
– Similar to everyday English
• Not executed on computers
– Used to think out program before coding
• Easy to convert into C++ program
– Only executable statements
• No need to declare variables
• C++ keywords
– Cannot be used as identifiers or variable names
C++ Keywords
Keywords common to the
C and C++ programming
languages
auto break case char const
continue default do double else
enum extern float for goto
if int long register return
short signed sizeof static struct
switch typedef union unsigned void
volatile while
C++ only keywords
asm bool catch class const_cast
delete dynamic_cast explicit false friend
inline mutable namespace new operator
private protected public reinterpret_cast
static_cast template this throw true
try typeid typename using virtual
wchar_t
• Flowchart
– Graphical representation of an algorithm
– Special-purpose symbols connected by arrows (flowlines)
– Rectangle symbol (action symbol)
• Any type of action
– Oval symbol
• Beginning or end of a program, or a section of code (circles)
• Single-entry/single-exit control structures
– Connect exit point of one to entry point of the next
– Control structure stacking
• Selection structure
– Choose among alternative courses of action
– Pseudocode example:
If student’s grade is greater than or equal to 60
Print “Passed”
– If the condition is true
• Print statement executed, program continues to next statement
– If the condition is false
• Print statement ignored, program continues
– Indenting makes programs easier to read
• C++ ignores whitespace characters (tabs, spaces, etc.)
if ( grade >= 60 )
cout << "Passed";
• Diamond symbol (decision symbol)
– Indicates decision is to be made
– Contains an expression that can be true or false
• Test condition, follow path
• if structure
– Single-entry/single-exit
• if
– Performs action if condition true
• if/else
– Different actions if conditions true or false
• Pseudocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”
• C++ code
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
2003 Prentice Hall, Inc. All rights reserved.
41
if/else Selection Structure
false true
grade >= 60
• Example
if ( grade >= 90 ) // 90 and above
cout << "A";
else if ( grade >= 80 ) // 80-89
cout << "B";
else if ( grade >= 70 ) // 70-79
cout << "C";
else if ( grade >= 60 ) // 60-69
cout << "D";
else // less than 60
cout << "F";
• Compound statement
– Set of statements within a pair of braces
if ( grade >= 60 )
cout << "Passed.\n";
else {
cout << "Failed.\n";
cout << "You must take this course again.\n";
}
– Without braces,
cout << "You must take this course again.\n";
always executed
• Block
– Set of statements within braces
• Repetition structure
– Action repeated while some condition remains true
– Psuedocode
while there are more items on my shopping list
Purchase next item and cross it off my list
– while loop repeated until condition becomes false
• Example
int product = 2;
while ( product <= 1000 )
product = 2 * product;
true
product <= 1000 product = 2 * product
false
• Counter-controlled repetition
– Loop repeated until counter reaches certain value
• Definite repetition
– Number of repetitions known
• Example
A class of ten students took a quiz. The grades (integers in
the range 0 to 100) for this quiz are available to you.
Determine the class average on the quiz.
Enter grade: 98
Enter grade: 76
Enter grade: 71
Enter grade: 87
Enter grade: 83
Enter grade: 90
Enter grade: 57
Enter grade: 79
Enter grade: 82
Enter grade: 94
Class average is 81 2003 Prentice Hall, Inc.
All rights reserved.
50
SentinelControlled Repetition
Include <iostream>
switch ( variable ) {
case value1: // taken if variable == value1
statements
break; // necessary to exit switch
case value2:
case value3: // taken if variable == value2 or == value3
statements
break;
true
case a case a action(s) break
false
true
case b case b action(s) break
false
.
.
.
true
case z case z action(s) break
false
default action(s)
• Example upcoming
– Program to read grades (A-F)
– Display number of each grade entered
• Details about characters
– Single characters typically stored in a char data type
• char a 1-byte integer, so chars can be stored as ints
– Can treat character as int or char
• 97 is the numerical representation of lowercase ‘a’ (ASCII)
• Use single quotes to get numerical representation of character
cout << "The character (" << 'a' << ") has the value "
true
condition
false
1 2 3 4 5 6 7 8 9 10
• break statement
– Immediate exit from while, for, do/while, switch
– Program continues with first statement after structure
• Common uses
– Escape early from a loop
– Skip the remainder of switch
• Structured programming
– Programs easier to understand, test, debug and modify
• Rules for structured programming
– Only use single-entry/single-exit control structures
– Rules
1) Begin with the “simplest flowchart”
2) Any rectangle (action) can be replaced by two rectangles
(actions) in sequence
3) Any rectangle (action) can be replaced by any control
structure (sequence, if, if/else, switch, while, do/while or for)
4) Rules 2 and 3 can be applied in any order and multiple times
Rule 3
Rule 3 Rule 3