Lecture 2
Lecture 2
Programming Fundamentals
Programming Languages
Brief History of C/C++
Input, Processing and output
Why C++
Program
“A precise
sequence of
steps to
solve a
particular
problem”
Computer Programming
The functions of a computer system are
controlled by computer programs
A computer program is a clear, step-by-step,
finite set of instructions
• Both
Programming Languages
Programmers write instructions in various programming
languages, some directly understandable by computers and
others requiring intermediate translation steps.
There are three categories of programming languages:
Low level Language
Machine Language
Intermediate Language
Assembly Language
High-level languages.
C, C++, Python, etc.
Programming Languages
Any computer can directly understand only its own machine
language (also called machine code), defined by its hardware
architecture
A Machine language program is written in as strings of 1s and 0s
which does not need translation.
Each kind of CPU has its own machine language.
Advantages
Fast execution and efficient
Written in binary
No translation required
Disadvantages
Machine dependent & not portable (Different for every CPU)
Not programmer friendly
Difficult to program/modify.
Assembly Language
Programming in machine language was slow and tedious. English-like
abbreviations were used to represent elementary operations which
formed the basis of assembly languages. Assembler was
developed for translation to machine code.
Assembly language programs use mnemonics to represent machine
instructions
Each statement in assembly language corresponds to one statement in
machine language.
Easier to program and modify compared to machine language.
It requires hardware knowledge and is machine dependent.
Compare the following machine and assembly language programs:
8086 Machine language program for 8086 Assembly program
var1 = var1 + var2 ; for
var1 = var1 + var2 ;
1010 0001 0000 0000 0000 0000 MOV AX , var1
0000 0011 0000 0110 0000 0000 ADD AX , var2
0000 0010 MOV var1 , AX
1010 0011 0000 0000 0000 0000
High-Level Languages
To speed up the programming process further, high-level languages
were developed in which single statements could be written to
accomplish substantial tasks.
A high-level language (HLL) has two primary components
(1) a set of built-in language primitives and grammatical rules
(2) a translator/compiler for syntax errors and translation
Compilers can not detect logical errors.
A HLL language program consists of English-like statements that are
governed by a strict syntax.
Advantages
Portable or machine independent
Programmer-friendly
Disadvantages
Not as efficient as low-level languages
Need to be translated
Some Well-Known Programming
Languages
C++
BASIC Ruby
FORTRAN
Java
Visual Basic
COBOL
C#
JavaScript
Python
Language Description
BASIC Beginners All-purpose Symbolic Instruction Code. A general programming language
originally designed to be simple enough for beginners to learn.
Ruby It is created in 1990s. It has become popular for programs that run on Web Servers.
Programming Paradigms
Why are there hundreds of programming languages in use today?
Some programming languages are specifically designed for use in
certain applications.
Different programming languages follow different approaches to
solving programming problems
A programming paradigm is an approach to solving programming
problems.
A programming paradigm may consist of many programming languages.
Common programming paradigms:
Imperative or Procedural Programming
Structured Programming
Object-Oriented Programming
Why Do We Need OOP
OOP was developed because limitations were discovered in
earlier approaches to programming.
C, Pascal, FORTRAN, and similar languages are procedural
languages.
• Each statement in the language tells the computer to do
something: Get some input, add these numbers, divide by six,
display that output.
A program in a procedural language is a list of instructions.
The programmer creates the list of instructions, and the
computer carries them out.
However, few programmers can comprehend a program of more
than a few hundred statements.
Functions in C/C++ are used as a way to make programs more
comprehensible to their human creators.
A procedural program is divided into functions, and each function
has a clearly defined purpose and a clearly defined interface to
the other functions in the program.
Why Do We Need OOP
The idea of breaking a program into functions can be further extended by
grouping a number of functions together into a larger entity called a
module (which is often a file), but the principle is similar: a grouping of
components that execute lists of instructions.
Dividing a program into functions and modules is one of the cornerstones
of structured programming.
Problems: Two problems with Procedural Paradigms.
1. Functions have unrestricted access to global data.
2. Unrelated functions and data, the basis of the procedural paradigm,
provide a poor model of the real world.
Local data is hidden inside a function, and is used exclusively by the
function.
However, when two or more functions must access the same data then
the data must be made global. Global data can be accessed by any
function in the program.
OOP Approach
The fundamental idea behind object-oriented languages is to combine
into a single unit both data and the functions that operate on that data.
Such a unit is called an object.
An object’s functions, called member functions in C++, typically provide
the only way to access its data.
Data and its functions are said to be encapsulated into a single entity.
Data encapsulation and data hiding are the key terms in the description
of object-oriented languages.
You can modify the object through member functions only.
No other functions can access the data. This simplifies writing,
debugging, and maintaining the program.
A C++ program typically consists of a number of objects, which
communicate with each other by calling one another’s member functions
From a High-Level Program to
an Executable File
a) Create file containing the program with a text editor.
b) Run preprocessor to convert source file directives to source code
program statements.
c) Run compiler to convert source program into machine instructions.
d) Run linker to connect hardware-specific code to machine instructions,
producing an executable file.
• Steps b–d are often performed by a single command or button click.
• Errors detected at any step will prevent execution of following steps.
Three Program Stages
myprog.cc myprog.obj myprog.exe
SOURCE OBJECT EXECUTABLE
written in
written in written in
machine
C++ machine
language
language
other code
from libraries,
etc.
From a High-Level Program to
an Executable File
C++ systems generally consist of three parts: a program
development environment, the language and the C++ Standard
Library.
C++ programs typically go through six phases:
1. edit
2. preprocess
3. compile
4. link
5. load and
6. execute
Editor
Phase 1 consists of editing a file with an editor program,
normally known simply as an editor.
You type a C++ program (referred to as source code)
using the editor, make any necessary corrections and
save the program on your computer’s disk.
C++ source code filenames often ends with the .cpp,.cc
or .C (uppercase) extensions which indicate that a file
contains C++ source code.
IDEs are available from major software suppliers.
Preprocessor
In Phase 2, you give the command to compile the
program.
In a C++ system, a preprocessor program executes
automatically before the compiler’s translation phase
begins.
The C++ preprocessor obeys commands called
preprocessing directives, which indicate that certain
manipulations are to be performed on the program
before compilation, i.e., other files to be compiled, and
perform various text replacements.
Directives start with # in the beginning of program
#define TABLE_SIZE 100
e.g. # int table1[TABLE_SIZE];
int table2[TABLE_SIZE];
Compiler