0% found this document useful (0 votes)
10 views36 pages

PF - 4

This document discusses the evolution of programming languages from machine language to high-level languages like C++. It covers the basic components of a C++ program including compilers that translate source code to machine language and linkers and loaders that combine object code files. The history of C and C++ is summarized, noting their development alongside operating systems. Structured and object-oriented programming styles are introduced.

Uploaded by

Saad Nadeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views36 pages

PF - 4

This document discusses the evolution of programming languages from machine language to high-level languages like C++. It covers the basic components of a C++ program including compilers that translate source code to machine language and linkers and loaders that combine object code files. The history of C and C++ is summarized, noting their development alongside operating systems. Structured and object-oriented programming styles are introduced.

Uploaded by

Saad Nadeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

CS1002 – Programming

Fundamentals
4
2
Basic Components of C++ Program
4 The Evolution of Programming
Languages
 Early computers were programmed in machine language
 To calculate wages = rates * hours in machine language:

100100 010001 //Load rates


100110 010010 //Multiply
100010 010011 //Store in wages
5 The Evolution of Programming
Languages (cont'd.)
 Assembly language instructions are mnemonic
 Assembler: Translates a program written in assembly language into
machine language
6 The Evolution of Programming
Languages (cont'd.)
 Using assembly language instructions,
wages = rates * hours can be written as:

LOAD rate
MULT hour
STOR wages
7 The Evolution of Programming
Languages (cont'd.)
 High-level languages include Basic, FORTRAN, COBOL, Pascal, C, C++, C#,
and Java
 Compiler: Translates a program written in a high-level language to machine
language
 The equation wages = rate • hours can be written in C++ as:
wages = rate * hours ;
8 Assembly & Machine Language
Assembly Language Machine Language

ST 1,[801] 00100101 11010011


00100100 11010100
ST 0,[802]
10001010 01001001 11110000
TOP: BEQ [802],10,BOT
01000100 01010100
INCR [802]
01001000 10100111 10100011
MUL [801],2,[803] 11100101 10101011 00000010 00101001
ST [803],[801] 11010101
JMP TOP 11010100 10101000
BOT: LD A,[801] 10010001 01000100

CALL PRINT
9 Equivalent C/C++ program

set memory[801] to hold 00000001 x=1;


set memory[802] to hold 00000000 i=0;
if memory[802] = 10 jump to instruction #8 while (i!=10) {
increment memory[802] i = i+1;

}
set memory[803] to 2 times memory[801] x = x*2;
put memory[803] in to memory[801] }
jump to instruction #3

print memory[801] printf("%d",x);


10 Compiler

C++ Program Machine


C++ Compiler Language
Program
int main() {
int i=1;
. . . 01001001
10010100

Created with text editor or


development environment
Processing a C++ Program
11
Basics of a Typical C++ Environment
Program is created in
12 Editor Disk the editor and stored
on disk

Preprocessor
Preprocessor Disk program processes
Phases of C++ Programs: the code

1. Edit Compiler Disk


Compiler creates
object code and
stores it on disk.

2. Preprocess Linker links the object


Linker Disk code with the libraries,
3. Compile creates a .exe and
stores it on disk
Primary memory

4. Link Loader

5. Load .
Loader puts program
in memory
.
Disk
6. Execute
.

Primary memory
CPU takes each
instruction and
CPU executes it, possibly
storing new data
.
.
values as the program
. executes.
13 Compilers
Translate high-level language to machine
language
Check that the program obeys the rules

Source code
The original program in a high-level language
Object code
The translated version in machine language
14 Linkers and Loader
 Some programs we use are already compiled
 Their object code is available for us to use
 For example: Input and output routines
 A Linker combines
 The object code for the programs we write
and
 The object code for the pre-compiled routines (of SDK)
into
 The machine language program the CPU can run
 Loader:
 Loads executable program into main memory
 The last step is to execute the program
Program Analysis Execution Coding Cycle

15
20 History of C and C++
 History of C
 Evolved from two other programming languages
 BCPL and B: “Typeless” languages
 Dennis Ritchie (Bell Lab): Added typing, other features
 C is a programming language developed in the 1970's alongside the UNIX
operating system
 C provides a comprehensive set of features for handling a wide variety of
applications, such as systems development and scientific computation
 1989: ANSI standard/ ANSI/ISO 9899: 1990
21 History of C and C++
 History of C++
 Early 1980s: Bjarne Stroustrup (Bell Lab)
 Provides capabilities for object-oriented programming
 Objects: reusable software components
 Object-oriented programs

 “Building block approach” to creating programs


 C++ programs are built from pieces called classes and functions
 C++ standard library: Rich collections of existing classes and functions
22 Structured/OO Programming
 Structured programming (1960s)
 Disciplined approach to writing programs
 Clear, easy to test and debug, and easy to modify
 e.g. Pascal: 1971: Niklaus Wirth
 OOP
 “Software reuse”
 “Modularity”
 “Extensible”
 More understandable, better organized and easier to maintain than procedural
programming
23 Basics of a Typical C++ Environment

C++ systems
Program-development environment
Integrated Development Environment (IDE)
Language
C++ Standard Library
C++ program names extensions
.cpp (C Plus Plus)
.c (C)
24 The C++ Standard Library
C/C++ programs consist of pieces/modules called functions
 A programmer can create his own functions
 Advantage: the programmer knows exactly how it works
 Disadvantage: time consuming
 Programmers will often use the C/C++ library functions
 Use these as building blocks
 Avoid re-inventing the wheel
 If a pre-made function exists, generally best to use it rather than write
your own
 Library functions carefully written, efficient, and portable
25 Programming Style

C++ is a free-format language, which means that:


 Extra blanks (spaces) or tabs before or after
identifiers/operators are ignored
 Blank lines are ignored by the compiler just like comments
 Code can be indented in any way
 There can be more than one statement on a single line
 A single statement can continue over several lines
26 Programming Style (cont. )
In order to improve the readability of your program, use the following
conventions:
 Start the program with a header that tells what the program does
 Use meaningful variable names and Camel notation
 Document each variable declaration with a comment telling what
the variable is used for
 Place each executable statement on a single line
 A segment of code is a sequence of executable statements that
belong together
 Use blank lines to separate different segments of code
 Document each segment of code with a comment telling what the
segment does.
27 C++ keywords
 Keywords appear in blue in Visual C++
 Each keyword has a predefined purpose in the language
 Do not use keywords as variable and constant names!!

 We shall cover most of the following keywords in this class:

bool, break, case, char, const, continue, do, default, double,


else, extern, false, float, for, if, int, long, namespace, return,
short, static, struct, switch, typedef, true, unsigned, void,
while
28 Structure of a C++ Program
A C++ program is a collection of definitions and
declarations:

 data type definitions


 global data declarations
 function definitions (subroutines)
 class definitions
 a special function called
 main() (where the action starts)
29 General form of a C++ program
// Program description
#include directives
global declarations
int main()
{
constant declarations
variable declarations
executable statements
return 0;
}
Preprocessor Directives
32
 Preprocessor directives: Begin with #
 Processed before compiling
 #include
 #define
 #define is used to define macros and constants in your program.
 #include is used to link to external header files (libraries) which may be
required in your program.
 The statement: #include <iostream> inserts the contents of the file iostream
inside your file before the compiler starts
 Definitions that allow your program to use the functions and classes that
make up the standard C++ library are in these files.
 You can include your own file(s):
 #include "myfile.h"
C++ compiler directives
33
 Lines that start with the character '#' are special instructions to a
preprocessor
 The preprocessor can replace the line with something else:
 include: replaced with contents of a file
 Other directives tell the preprocessor to look for patterns in the program
and do some fancy processing
 Compiler directives appear in blue in Visual C++
 The #include directive tells the compiler to include some already existing
C++ code in your program
 The included file is then linked with the program
 There are two forms of #include statements:
#include <iostream> //for pre-defined files
#include "my_lib.h" //for user-defined files
34 C++ Preprocessor

 C++ Compilers automatically invoke a preprocessor that


takes care of #include statements and some other
special directives
 Definitions that allow your program to use the functions
and classes that make up the standard C++ library are in
these files
 You don't need to do anything special to run the
preprocessor - it happens automatically
35 Preprocessing

Temporary file
C++ (C++ program) C++
Preprocessor Compiler

Executable
C++ Program Program
36 Some common include statements
 Basic I/O: iostream.h
 Provides functionality of input and output
 I/O manipulation: iomanip.h
 Format’s the input and output
 Standard Library: stdlib.h
 Functions for memory allocation, process control, conversion
etc.
 Time and Date support: time.h
 Functionality of time manipulation
 Mathematics support: math.h
 Functionality of basic mathematical functions
37 C++ …
 Explore simple data types
 Discover how to use arithmetic operators
 Examine how a program evaluates arithmetic
expressions
 Learn what an assignment statement is and what it does
 Become familiar with the string data type
 Become familiar with the use of increment and
decrement operators
 Explore how to properly structure a program, including
using comments to document a program
 Learn how to write a C++ program
38 Testing and debugging

 Bug
 A logical 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.”
39 Testing and debugging
FINALLY! A Programmers Drinking Song! Woo! Hoo!

PROGRAMMERS DRINKING SONG:

99 little bugs in the code,


99 bugs in the code,
fix one bug, compile it again,
101 little bugs in the code.
101 little bugs in the code.....
(Repeat until BUGS = 0)
40 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 as an error
41 What makes a bad program?

Writing Code without detailed analysis and design


Repeating trial and error without understanding
the problem
Debugging the program line by line, statement by
statement
Writing tricky and dirty programs
42 Live cycle of a program
43

Thank you

You might also like