CSI-302
PROGRAMMING FUNDAMENTALS
LECTURE # 5
E VOL UT I ON OF P ROG R AMMI NG L ANG UAGES
• 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
2
E VOL UT I ON OF P ROG R AMMI NG L ANG UAGES …
• Assembly language instructions are mnemonic
• Assembler: Translates a program written in assembly language
into machine language
3
E VOL UT I ON OF P ROG R AMMI NG L ANG UAGES …
• Using assembly language instructions,
wages = rates * hours can be written as:
LOAD rate
MULT hour
STOR wages
4
E VOL UT I ON OF P ROG R AMMI NG L ANG UAGES …
• 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 ;
5
ASSE MB LY & MACH I NE L ANG UAGE
Assembly Language Machine Language
ST 1,[801] 00100101 11010011
ST 0,[802] 00100100 11010100
TOP: BEQ [802],10,BOT 10001010 01001001 11110000
INCR [802] 01000100 01010100
MUL [801],2,[803] 01001000 10100111 10100011
ST [803],[801] 11100101 10101011 00000010 00101001
JMP TOP 11010101
BOT: LD A,[801] 11010100 10101000
CALL PRINT
10010001 01000100
6
COMP I L E R
C++ Program Machine
C++ Compiler Language
Program
int main() {
int i=1;
. . . 01001001
10010100
Created with text editor or
development environment
7
COMP I L E R
• 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
8
L I NK ER S
• 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
9
H I STORY OF C & 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
10
H I STORY OF C & C+ +
• History of C++
• Early 1980s: at 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
11
ST RUCT UR ED / OB J E CT-OR I ENTED P ROG R AMMI NG
• 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
12
B ASI CS OF C+ + E NV I RONMENT
• C++ systems
• Program-development environment
• Integrated Development Environment (IDE)
• Language
• C++ Standard Library
• C++ program names extensions
• .cpp (C Plus Plus)
• .c (C)
13
C+ + STANDAR D L I BR ARY
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
14
PROG R AMMI NG ST Y L E
C++ is a free-format language:
• 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
15
PROG R AMMI NG ST Y L E ( CONT. )
Improve readability of the program, by using following conventions:
• Start the program with a header
• 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.
16
C+ + K E Y WOR DS
• 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
17
ST RUCT UR E OF A C+ + P ROG R AM
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)
18
G E NER AL FOR M OF A C+ + P ROG R AM
// Program description
#include directives
global declarations
int main()
{
constant declarations
variable declarations
executable statements
return 0;
}
19
I NCL UDES
• #include <iostream> inserts contents of file iostream inside
your file before the compiler starts
• You can include your own file(s):
#include "myfile.h"
21
C+ + COMP I L E R DI R E CT IV ES
• 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
22
P R E P ROC E SSOR
• Lines that start with character '#' are special instructions to a
preprocessor
• The preprocessor can replace 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
23
I NCL UDE STAT E ME NT S
• 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
25
T E ST I NG AND DE B UGGING
• Bug
• A logical mistake in a program
• Debugging
• Eliminating mistakes in programs
26
P ROG R A M E R ROR S
• 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
• Logical errors
• Errors in the program’s algorithm
• Most difficult to diagnose
• Computer does not recognize as an error
27
B AD PR ACT I CE OF P ROG R AMMI NG
• 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
28
DATA T Y P E S
• Data type: Set of values together with a set of operations
• C++ data types fall into three categories:
29
SI MP L E DATA T Y P E S
Three categories of simple data
• Integral: integers (numbers without a decimal)
• Floating-point: decimal numbers
• Enumeration type: user-defined data type
30
SI MPL E DATA T Y P E S ( CONT ’ D. )
Integral data types are further classified into nine categories:
• char, short, int, long, bool
• unsigned char, unsigned short, unsigned int, unsigned long
31
SI MPL E DATA T Y P E S ( CONT ’ D. )
• Different compilers may allow different ranges of values
Data Type Values Storage (in Bytes)
int -2147483648 to 2147483647 4
bool true and false 1
char -128 to 127 1
32
I NT DATA T Y P E
Examples:
-6728
0
78
+763
• Positive integers do not need a + sign
• No commas are used within an integer [76,385]
• Commas are used for separating items in a list
33
B OOL DATA T Y P E
• bool type
• Two values: true and false
• Manipulate logical (Boolean) expressions
• true and false
• Logical values
• bool, true, and false
• Reserved words
34
CH AR DATA T Y P E
• The smallest integral data type
• Used for characters: letters, digits, and special symbols
• Each character is enclosed in single quotes
• 'A', 'a', '0', '*', '+', '$', '&'
• A blank space is a character
• Written ' ', with a space left between the single quotes
• ‘abc’ and ‘!=‘ are not char
35
FL OAT DATA T Y P E
• Used for scientific notation. For example:
• 43872918 = 4.3872918 * 107
• .0000265 = 2.65 * 10-5
• 47.9832 = 4.79832 * 101
36
FL OAT DATA T Y P E
• C++ uses scientific notation to represent real numbers
(floating-point notation)
Real Number C++ Floating-Point Notation
75.924 7.592400E1
0.18 1.800000E-1
0.0000453 4.530000E-5
-1.482 -1.482000E0
7800.0 7.800000E3
37
DATA T Y P E S AND VAR I AB L ES
• To declare a variable, we not only specify variable but also specify
type of data a variable can store.
• A syntax rule to declare a variable
datatype identifier;
• For example, consider the following examples:
int counter;
double interestRate;
char grade;
38
END