Lecture1 IntroToOOP
Lecture1 IntroToOOP
CMT210: OOP1
2
1). Machine Language
Comprised of 1s and 0s
Example of code:
1110100010101 111010101110
10111010110100 10100011110111
3
Advantages of 1GL
They are translation free and can be directly executed by the
computers.
4
2). Assembly Language
Assembly languages are a step towards easier programming.
Example:
ADD 1001010, 1011010
5
Assembly Language contd.
A low-level processor-specific
programming language design
to match the processor’s
machine instruction set
6
Why learn Assembly Language?
To learn how high-level language code gets translated into machine
language
i.e.: to learn the details hidden in HLL code
To speed up applications
direct access to hardware (ex: writing directly to I/O ports instead of
doing a system call)
good ASM code is faster and smaller: rewrite in ASM the critical areas
of code
7
Assembly Language Applications
Application programs are rarely written completely in assembly
language
only time-critical parts are written in ASM
Ex1: an interface subroutine (called from HLL programs) is written in ASM for
direct hardware access
Ex2: device drivers (called from the OS)
ASM often used for embedded systems (programs stored in PROM chips)
computer cartridge games, microcontrollers (automobiles, industrial plants...),
telecommunication equipment…
8
Assembler
An assembler is a program that converts ASM code into
machine language code:
mov al,5 (Assembly Language)
1011000000000101 (Machine Language)
most significant byte is the opcode for “move into register AL”
the least significant byte is for the operand “5”
9
Advantages of 2GL
It is easy to develop, understand and modify the program
developed in these languages as compared to those developed
in the first generation programming language.
10
3). High-Level Languages
High-level languages represent a giant leap towards easier
programming.
11
High-Level Languages contd.
They are called high-level because they are closer to human languages
and further from machine languages.
In contrast, assembly languages are considered low-level because they
are very close to machine languages.
12
Advantages of 3GL
It is easy to develop, learn and understand the program.
13
Examples of 3GL
FORTRAN, ALGOL, COBOL, C++, C, Java, Visual Basic,
JavaScript etc
14
4). 4GL (Very High-level Languages)
The languages of this generation were considered as very
high-level programming languages requiring a lot of time
and effort that affected the productivity of a programmer.
15
Advantages of 4GL
These programming languages allow the efficient use of data by
implementing various databases.
They require less time, cost and effort to develop different types
of software applications.
16
Examples of 4GL
Perl, PHP, Python, Ruby, SQL etc
17
5GL (Artificial Intelligence Language)
The programming languages of this generation mainly focus on
constraint programming.
18
Advantages of 5GL
These languages can be used to query the database in a fast
and efficient manner.
19
Programming Languages chart
20
Programming Paradigms
A programming paradigm is a style, or “way,” of programming.
21
Programming Paradigms
Paradigms differ in the concepts and
abstractions used to represent the elements of a
program (such as objects, functions, variables,
constraints, etc.) and the steps that compose a
computation (assignment, evaluation,
continuations, data flows, etc.).
22
Programming Paradigms contd.
There are lots of programming languages that are known but all
of them need to follow some strategy when they are
implemented. This methodology/strategy is what is called a
Programming paradigm.
23
Programming Paradigm examples
Imperative programming paradigm
Procedural programming paradigm
Declarative programming paradigm
Logical programming paradigm
Functional programming paradigm
Structured programming paradigm
Object-oriented programming paradigm
Etc, etc
24
Some paradigms explained
Imperative: Programming with an explicit sequence of
commands that update state.
Declarative: Programming by specifying the result you want, not
how to get it.
Structured: Programming with clean, goto-free, nested control
structures.
Procedural: Imperative programming with procedure calls.
Object-oriented: a problem is divided into a number of objects
which can interact by passing messages to each other
25
STRUCTURED vs. OO PROGRAMMING
FUNCTION 4 FUNCTION 5
26
Structured Programming
Using function
Function & program is divided into
modules
Every module has its own data and
function which can be called by other
modules.
27
OBJECT ORIENTED PROGRAMMING
Object 2
Object 1
Data Data
Function Function
Object 3
Data
Function
28
OBJECT ORIENTED PROGRAMMING
Objects have both data and methods
Objects of the same class have the same data elements
and methods
Objects send and receive messages to invoke actions
29
OOP TERMINOLOGIES
object
- usually a person, place or thing (a noun)
method
- an action performed by an object (a verb)
attribute
- description of objects in a class
class
- a category of similar objects (such as automobiles)
- does not hold any values of the object’s attributes
30
Examples of attributes and methods
Attributes: Methods:
manufacturer’s name Define data items
model name (specify manufacturer’s
year made name, model, year,
color etc.)
number of doors Change a data item
size of engine
(color, engine, etc.)
Display data items
etc.
Calculate cost
etc.
31
Why OOP?
Save development time (and cost) by
reusing code
once an object class is created it can be used
in other applications
Easier debugging
classes can be tested independently
reused objects have already been tested
32
Design Principles of OOP
The 4 basic design principles of Object-Oriented
Programming (OOP):
Encapsulation
Abstraction
Polymorphism
Inheritance
33
1). Encapsulation
The bundling of data and methods
operating on this data into one unit (called
a class).
34
2). Abstraction
Focus only on the important facts about the
problem at hand
to design, produce and describe so that it can be
easily used without knowing the details of how it
works.
Analogy:
When you drive a car, you don’t have to know how
the gasoline and air are mixed and ignited.
Instead you only have to know how to use the
35
controls.
3). Polymorphism
Greek for “many forms”
The same word or phrase can mean different things in different
contexts.
The ability of a variable, function, or object to take on multiple
forms.
Class objects belonging to the same hierarchical tree may have
methods with the same name, but with different behaviours.
Analogy:
In English, bank can mean side of a river or a place to put
money
36
3). Polymorphism Contd.
a). Method Overloading
The operation of one function depends on the argument
passed to it.
Example: Fly(), Fly(low), Fly(150)
37
4). Inheritance
Inheritance—a way of organizing classes
Term comes from inheritance of traits like eye color, hair
color, and so on.
Classes with properties in common can be grouped so
that their common properties are only defined once.
Superclass – inherit its attributes & methods to the
subclass(es).
Subclass – can inherit all its superclass attributes &
methods besides having its own unique attributes &
methods.
38
An Inheritance Hierarchy
Superclass
Vehicle
Subclasses
40
Classes
A construct that is used as a blueprint to
create instances of the class (class instances,
class objects, instance objects or just
objects).
It defines constituent members which enable
class instances to have state and behavior.
It is an instance of class
43