First Steps with C++
CSCI 1205: Intro to Programming in C++
Lecture 02
Aleksandar Zoranic
University of New Orleans
Slides adapted from Dr. Daniel Bilar 1
Goals Today
Appreciation of Program Design Process
Parsing a very simple C++ program
Programming concepts
Libraries:iostream
Main function: int main()
C++ instructions: cin, cout, return
Using our C++ IDE
Foray into testing and debugging
Syntax (compile-time), execution (run-time), logic
errors
2
Review: Programming Concepts
Algorithm
A sequence of precise
instructions that
leads to a solution
Ex: in the picture
Program
An algorithm expressed in
a language the
computer can understand
Ex: C++, Java, Assembly,
Lisp, Visual Basic etc
http://computer.howstuffworks.com/routing-algorithm3.htm 3
Software Life Cycle
6 stages
1. Analysis and specification of the task
(problem definition)
2. Design of the software
(object and algorithm design)
3. Implementation (coding)
4. Testing
5. Maintenance and evolution of the system
6. Obsolescence
4
Program Design
Programming is a creative
process
No complete set of rules for
creating a program
Program Design Process
Problem Solving Phase
Result is a description that solves
the problem
First English, then English-
algorithmic
Implementation Phase
Result is the algorithm translated
into a programming
language
5
Problem Solving Phase
Be certain the task is
completely specified
What is the input?
What information is in the
output?
What error conditions can
arise?
How can I test easily and
comprehensively?
What output am I returning?
Work with the algorithm
before coding
Experience shows this saves a
lot of time
6
Implementation Phase
Translating to C++
Code the algorithm in a
programming language. This gets
easier as you gain experience with
the language
Compile the source code
Locates errors in using the
programming language
Testing:
Run the program on sample data,
test harness
Verify correctness of results
Results may require modification
of the algorithm and program
Software bugs cost the US economy
an estimated $59 billion annually
(2002) – 0.6% of GDP
7
Challenges of Software Engineering
Software is prone to malliciousness
Software construction is human-intensive
Software is intangible
Software problems are unprecedentedly
complex
Software solutions require unusual rigor
Software has discontinuous operational
nature
Object Oriented Programming
Used for many modern programs
C++ is a language that enables OOP
Paradigm
Program is viewed as interacting objects
Each object contains state variables and
algorithms to describe its behavior and for
interaction with other objects
Program design phase involves designing
objects and their algorithms
Characteristics
Encapsulation
Information hiding
Objects contain their own data and algorithms
Inheritance
Writing reusable code
Objects can inherit characteristics from other
objects
Polymorphism
A single name can have multiple meanings
depending
on its context Picture from Zeegee software
9
Introduction to C++
Where did C++ come from?
Derived from the C language
C was derived from the B language
B was derived from the BCPL language
Why the ‘++’?
++ is an operator in C++ and results in a cute pun
10
C++ History
C developed by Dennis Ritchie at AT&T
Bell Labs in the 1970s.
Used to maintain UNIX systems
Many commercial applications written in c
C++ developed by Bjarne Stroustrup at AT&T
Bell Labs in the 1980s.
Overcame several shortcomings of C
Incorporated object oriented programming
C remains a subset of C++
11
A Sample C++ Program
A simple C++ program begins this way
#include <iostream>
using namespace std;
int main()
{
And ends this way
return 0;
}
Display 1.8
12
Explanation of code (1/5)
Variable declaration line
int number_of_pods, peas_per_pod,
total_peas;
Identifies names of three variables to name numbers
int means that the variables represent integers
13
Explanation of code (2/5)
Program statement
cout << “Press return after entering a
number.\n”;
cout (see-out) used for output to the monitor
“<<“ inserts “Press…a number.\n” in the data
bound for the monitor
Think of cout as a name for the monitor
“<<“ points to where the data is to end up
‘\n’ causes a new line to be started on the monitor
14
Explanation of code (3/5)
Program statement
cin >> number_of_pods;
cin (see-in) used for input from the keyboard
“>>” extracts data from the keyboard
Think of cin as a name for the keyboard
“>>” points from the keyboard to a variable where the data
is stored
15
Explanation of code (4/5)
Program statement
total_peas = number_of_pods *
peas_per_pod;
Performs a computation
‘*’ is used for multiplication
‘=‘ causes total_peas to get a new value based
on the calculation shown on the right of the equal sign
16
Explanation of code (5/5)
Program statement
cout << number_of_pods;
Sends the value of variable
number_of_pods to the monitor
17
Program Layout (1/3)
Compiler accepts almost any pattern of line
breaks and indentation
Programmers format programs so they
are easy to read
Place opening brace ‘{‘ and closing brace ‘}’
on a line by themselves
Indent statements
Use only one statement per line
18
Program Layout (2/3)
Variables are declared before they are used
Typically variables are declared at the beginning of
the program
Statements (not always lines) end with a semi-colon
Include Directives
#include <iostream>
Tells compiler where to find information about items
used in the program
iostream is a library containing definitions of cin
and cout
19
Program Layout (3/3)
using namespace std;
Tells the compiler to use names in iostream in
a “standard” way
To begin the main function of the program
int main()
{
To end the main function
return 0;
}
Main function ends with a return statement
20
Running a C++ Program
C++ source code is written with a text
editor
The compiler on your system converts
source code to object code.
The linker combines all the object code
into an executable program.
21
Run a Program
Obtain code in Display 1.10 Display 1.10
Compile the code
Fix any errors the compiler indicates and
re-compile the code
Run the program
Now you know how to run a program on
your system
22
Section 1.3 Conclusion
Can you…
Describe the output of this line?
cout << “C++ is easy to understand.”;
Explain what this line does?
cin >> peas_per_pod;
Explain this? #include <iostream>
Next: Section 1.4 Testing and Debugging
23
Sec 1.4: Testing and Debugging
Bug
A mistake in a program
Debugging
Eliminating mistakes in programs
Term used when a moth caused a failed relay
on the Harvard Mark 1 computer. Grace Hopper
and other programmers taped the moth in logbook
stating:
“First actual case of a bug being found.”
24
Example of Software Failures
March 31, 1986: Mexicana 1991 Gulf War, failure of a Patriot
Airline, Boeing 727 crashed missile system to track an Iraqi
into a mountain Scud missile, killed 28 American
soldiers
March-June 1986: Therac 25
October 26, 1992, the ambulance
radiation therapy machines
dispatch system in London failed
overdosed cancer patients
after installation
November 2-4, 1988: Morris June 4, 1996, ESA’s Ariane 5
Computer virus rocket exploded 40 seconds after
September 17, 1991: A power liftoff
outage at AT&T switching December 10, 1990, Space Shuttle
facility in NYC interrupted Columbia was forced to land early
service to 10 million phone September 1999, Mars Climate
users for 9 hours Orbiter exploded on entry
See Neumann http://tinyurl.com/9vusas for a looong list of
software gone bad .. engineers take note
Program Errors
Syntax errors
Violation of the grammar rules of the language
Discovered by the compiler
Error messages may not always show correct location of
errors
Run-time errors
Error conditions detected by the computer at run-
time
Logic errors
Errors in the program’s algorithm
Most difficult to diagnose
Computer does not recognize an error
Let’s try to induce some errors with our compiler 26
Next class
Read Savitch, Ch 1, again
Peruse Savitch, Ch. 2
Download and Install Visual Studio C++
Express Edition 2008 for Windows
27