C++ Lect 3
C++ Lect 3
Ms Qurat-ul-Ann
1) Analyze the Problem
Brewster’s Thousands
The problem: Brewster wants to invest money at a local
bank. There are many options such as interest rates,
terms of deposit, compounding frequencies. He needs a
program to compute, for any given initial investment,
the final maturity (value) of the deposit.
What are the inputs? (given data)
What are the outputs? (required data)
How will we calculate the required outputs from the given
inputs?
Ms Qurat-ul-Ann
2) Design the Program
Create an outline of the program
An algorithm – a step by step procedure that will
provide the required results from the given inputs.
Algorithm Examples: Instructions on how to make a
cake, use the bank’s ATM, etc.
Ms Qurat-ul-Ann
3) Code the Program
Once the design is completed, write the program
code.
Code is written in some programming language such
as BASIC, Pascal, C++, Java, etc.
In this course we write code in pseudo-code,
developing the skills to be used when studying the
specific languages.
Ms Qurat-ul-Ann
4) Testing the program
Locate any errors (bugs)
Testing is done throughout the development cycle
Desk-checking, or code walkthrough is
performed to locate errors in the code.
Pretend you are the computer and execute your own code.
Ms Qurat-ul-Ann
Testing
Most of the work should be done before the phase
begins – creating of a testing document.
Two types of testing:
Testing for errors
Quality/Usability testing
Ms Qurat-ul-Ann
Documentation
Internal Documentation
Comments explain to the reader the logic and decision
processes of the programmer. Comments are ignored by
an interpreter or compiler.
Types of comments include code comments,
documentation comments & module comments.
External Documentation
External documentation includes a user’s guide and,
typically, a more technical system administrator’s guide.
Ms Qurat-ul-Ann
Object-Oriented Languages
Most object-oriented languages are high-level
languages.
The focus of OOP languages is not on structure, but
on modeling data.
Programmers code using “blueprints” of data models
called classes.
Examples of OOP languages include C++, Visual
Basic.NET and Java.
Ms Qurat-ul-Ann
Basics of a Typical C++ Environment Editor Disk
Program is created in
the editor and stored
on disk.
1. Edit Compiler
Compiler creates
object code and stores
Disk
2. Preprocess it on disk.
Linker links the object
3. Compile Linker Disk code with the libraries,
creates a.out and
4. Link Primary
Memory
stores it on disk
Loader
5. Load Loader puts program
6. Execute Disk ..
in memory.
..
..
Primary
Memory
CPU
CPU takes each
instruction and
executes it, possibly
storing new data
..
.. values as the program
..
executes.
Ms Qurat-ul-Ann
Creating and Editing a C++ Program
The Process of writing C++ program is known as editing. The process
Ms Qurat-ul-Ann
Creating and Editing a C++ Program
The edit window appears as follows:
Ms Qurat-ul-Ann
Creating and Editing a C++ Program
The edit window has a doubled lined border. The courser blinks in the
window.
The position of the cursor indicates the starting point to write a program.
The user can expand the window by clicking the arrow in the upper right
corner.
The user can also select Window>Zoom to expand the window. The vertical
Ms Qurat-ul-Ann
Saving a C++ Program
The process of storing the program on disk is known as Saving. A program
Ms Qurat-ul-Ann
Saving a C++ Program
The following procedure is used to save a C++ program:
1. Select File >Save OR Press F2 Key. The Save File As dialog box will appear.
2. Enter the file name. the default name NONAME.CPP appears in the dialog
box.
3. Enter the Path to save file. The default location to save program is BIN
directory.
4. Click on ok. The program will be saved at special location with specified
name.
Ms Qurat-ul-Ann
Saving a C++ Program
Ms Qurat-ul-Ann
Compiling a C++ Program
The Process of converting a Source Program into an object program
is known as compiling.
The program save with .cpp extension contains the statement of C++
the source program into object program and saves it in a separate file.
The object program is saved with .obj extension.
The source code cannot be compiled if contains any syntax errors.
2. The program will be translated into object code if it contains no errors. The
compiler will generate error message if the program contains any errors.
Ms Qurat-ul-Ann
Linking a C++ Program
The process of linking library files with the object program is known as linking.
These files are used to accomplish different tasks such as input/output etc..
A library files must be linked with the object file before execution of program.
A program that combines the object program with the additional library files is known as linker. It
A new file is created with .exe extension if the process of linking is successful. This files is known
Ms Qurat-ul-Ann
Executing a C++ Program
The process of running an executable file is known as executing.
The C++ program can be executed after compiling and linking. The program
or press ALT+F5
Ms Qurat-ul-Ann
Running Programs
• Steps that the computer goes through to run a
program:
Memory
Machine language
program
(executable file)
Input Data Data entered CPU
during execution
Computed results
Program Output
Ms Qurat-ul-Ann
The user enters program and save it as source file
Revised
Source File Source File
Success Failure
Errors List
Other Object
Linker links new object file with other object files
Files
Executable File
Different Steps from writing to
executing C++ program
Loader place executable file in memory
Ms Qurat-ul-Ann