0% found this document useful (0 votes)
51 views35 pages

Lecture 2

This document provides an overview of programming fundamentals and C/C++. It discusses programming languages from low-level machine language to high-level languages like C/C++. It covers a brief history of C and C++, and why object-oriented programming was developed. It also describes the process of compiling a high-level program into an executable file.

Uploaded by

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

Lecture 2

This document provides an overview of programming fundamentals and C/C++. It discusses programming languages from low-level machine language to high-level languages like C/C++. It covers a brief history of C and C++, and why object-oriented programming was developed. It also describes the process of compiling a high-level program into an executable file.

Uploaded by

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

Lecture-2

Programming Fundamentals

Syed Ameer Ahmed Gillani


Assistant Professor
[email protected]
Contents for Today’s Lecture

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

A computer program must be clear so that only


one meaning can be derived from it,

A computer program is written in a computer


language called a programming language
Introduction
C++ is a powerful Programming language that is
appropriate for:
people with little or no experience.
experienced programmers to use in building
substantial information systems
Current version is C++14 and is standardized by
ISO.
History
History of C and C++
• Initially, an older language BCPL was developed by Martin Richards,
which influenced another language known as B, which was invented by
Ken Thompson. B led to development of C in 1970s.
• Implemented by Dennis Ritchie (Bell Laboratories) in 1972
• Added data types
• Widely considered as a development language of UNIX
• Today, most of the code for general-purpose operating systems is written
in C or C++.
• A standard version of C was needed to provide a platform for developers
• 1989: ANSI standard
• 1990: ANSI and ISO standard published
• Current version is C11
• Hardware independent
• Portable programs
7
History of C and C++
• History of C++
• Extension of C
• Developed by Bjarne Stroustrup (Bell Laboratories) in 1979
• Early 1980s: Renamed to C++ or C with classes
• Provides capabilities for object-oriented programming inspired
from Simula language.
• C++ programs consist of pieces called classes and functions
• Objects: reusable software components
• Model items in real world
• Object-oriented programs
• Easy to understand, correct and modify
• Hybrid language
• C-like style
• Object-oriented style 8

• 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.

FORTRAN Formula Translator. A language designed for programming complex mathematical


algorithm.
COBOL Common Business-Oriented Language. A language designed for business application.

Pascal A structured, general-purpose language designed primarily for teaching programming.

C A structured, general-purpose language developed at Bell Laboratories. C offers both


high-level and low-level features.
C++ Based on the C language, C++ offers object-oriented features not found in C. Also
invented at Bell Laboratories.
C# Pronounced “C sharp”. A language invented by Microsoft for developing applications
based on the Microsoft .NET platform.
Java An object-oriented language invented at Sun Microsystems. Java may be used to develop
programs that run over the internet, in a web browser.
JavaScript JavaScript can be used to write small programs that run in Web pages.
Python It is created in 1990s. It has become popular in both business and academic application.

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

via compiler via linker

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

 In Phase 3, the compiler translates the C++ program


into machine language code—also referred to as
object code
Linker
 Phase 4 is called linking.
 C++ programs typically contain references to functions
and data defined elsewhere, such as in the standard
libraries or in the private libraries of groups of
programmers working on a particular project.
 The object code produced by the C++ compiler typically
contains “holes” due to these missing parts.
 A linker links the object code with the code for the
missing functions to produce an executable program.
 If the program compiles and links correctly, an executable
image is produced.
Loader & Execution
 Phase 5 is called loading. Before a program can be
executed, it must first be placed in memory.
 This is done by the loader, which takes the executable
image from disk and transfers it to memory.

 Finally, the computer, under the control of its CPU,


executes the program one instruction at a time
Integrated Development
Environments (IDEs)

• An integrated development environment, or IDE,


combine all the tools needed to write, compile, and
debug a program into a single software application.
• Examples are Microsoft Visual C++, Turbo C++ Explorer,
CodeWarrior, etc.
Integrated Development
Environments (IDEs)
1A-
34
Thank You!

You might also like