Lecture: 1 (Introduction)
Course
Object Oriented
Programming (OOP)
Instructor
Mr. Sarmad Shafique
Lecturer
Department of Computer Science
National University of Modern Languages
Programming Languages
2
Programming Languages
Programming languages allow programmers to code software.
The three major families of languages are:
Machine languages
Assembly languages
High-Level languages
3
Machine Languages
Comprised of 1s and 0s
The “native” language of a computer
Difficult to program – one misplaced 1 or 0 will cause the
program to fail.
Example of code:
1110100010101 111010101110
10111010110100 10100011110111
4
Assembly Languages
Assembly languages are a step towards easier programming.
Assembly languages are comprised of a set of elemental
commands which are tied to a specific processor.
Assembly language code needs to be translated to machine
language before the computer processes it.
Example:
ADD 1001010, 1011010
ADD AX,BX
5
High-Level Languages
High-level languages represent a giant leap towards
easier programming.
The syntax of HL languages is similar to English.
Historically, we divide HL languages into two groups:
Procedural languages
Object-Oriented languages (OOP)
6
Procedural Programming
7
Procedural Languages
Procedural programming is the standard approach used in
early High-Level computer language such as C, Pascal,
FORTRAN & BASIC.
Procedural programming creates a step-by-step program
that guides the application through a sequence of
instructions. Each instruction is executed in order.
Code in procedural languages is executed sequentially,
from top to bottom.
The program starts at the main entry point and follows a
linear path, executing each statement in order.
8
Procedural Languages
Using function
Function & program is divided into modules
Every module has its own data and function
which can be called by other modules.
9
TASK
Find errors in the program?
#include <iostream>
#include <stdio.h>
int main() {
cout << "Hello, World!" <<endl
return 0;
}
What is the output of this program.
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
int x = 5;
int y = x++;
cout << "x: " << x <<endl;
cout << "y: " << y <<endl;
return 0;
}
10
Object-Orientation
11
Object-Orientation
A thinking methodology
Everything is an object.
Any system is composed of objects (a system is also an
object).
The evolution and development of a system is caused by
the interactions of the objects inside/outside a system.
12
Everything is an object
A student, a professor
A desk, a chair, a classroom, a building
A university, a city, a country
The world, the universe
A subject such as CS, IS, Math, History, …
13
Systems are composed of objects
An educational system
An economic system
An information system
A computer system
14
Object-Oriented Languages
15
Object-Oriented Languages
Most object-oriented languages are high-level
languages.
The focus of OOP languages is not on structure, but
on modeling data.
Programmers code using “blueprints” of data models
called classes.
Examples of OOP languages include C++, Visual
Basic.NET and Java.
16
Basic terminology
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
17
Object-Oriented Programming
An object is a collection of data members and
associated member functions.
Each object is identified by a unique name. Each
object must be a member of a particular class.
Example: Apple, orange, mango are the objects of
class fruit.
Objects of the same class have the same data
elements and methods
Objects send and receive messages to invoke actions
Key idea in object-oriented:
The real world can be accurately described as a collection
of objects that interact.
18
Object-Oriented Programming
Object – Unique programming entity that has
methods, has attributes and can react to events.
Method – Things which an object can do; the “verbs”
of objects. In code, usually can be identified by an
“action” word -- Hide, Show
19
Object-Oriented Programming
Attribute – Things which describe an object; the
“adjectives” of objects. In code, usually can be
identified by a “descriptive” word – Enabled,
BackColor
Events – Events allow objects to notify and
communicate with other objects when certain actions
or state changes occur.
20
Object-Oriented Programming
Class – Provides a way to create new objects based
on a “meta-definition” of an object (Example: The
automobile class)
Constructors – Special methods used to create new
instances of a class (Example: A Honda Civic is an
instance of the automobile class.)
21
Object-Oriented Programming
Object 2
Object 1
Data Data
Function Function
Object 3
Data
Function
22
Procedural Programming vs OOP
A Real-World Example:
Let's say that you are working for a vehicle parts manufacturer that needs to
update it's online inventory system. Your boss tells you to program two similar
but separate forms for a website, one form that processes information about
cars and one that does the same for trucks.
For cars, we will need to record the following information: Color, Engine Size,
Transmission Type, Number of doors
For trucks, the information will be similar, but slightly different. We need:
Color, Engine Size, Transmission Type, Cab Size, Towing Capacity
23
Procedural Programming vs OOP
Scenario 1
Suppose that we suddenly need to add a bus form, that
records the following information: Color, Engine Size,
Transmission Type Number of passengers
Procedural: We need to recreate the entire form,
repeating the code for Color, Engine Size, and
Transmission Type.
OOP: We simply extend the vehicle class with a bus class
and add the method, numberOfPassengers.
24
Procedural Programming vs OOP
Scenario 2
Instead of storing color in a database like we previously did, for
some strange reason our client wants the color emailed to him.
Procedural: We change three different forms: cars, trucks, and
buses to email the color to the client rather than storing it in the
database.
OOP: We change the color method in the vehicle class and
because the car, truck, and bus classes all extend (or inherit
from, to put it another way) the vehicle class, they are
automatically updated.
25
Procedural Programming vs OOP
Scenario 3
We want to move from a generic car to specific makes, for
example: Nissan and Mazda.
Procedural: We create a new form for each make, repeating all
of the code for generic car information and adding the code
specific to each make.
OOP: We extend the car class with a Nissan class and a Mazda
class and add methods for each set of unique information for
that car make.
26
Procedural Programming vs OOP
Example: C++, Java, Python etc Example: C, Fortran, Pascal, Basic etc
27
Thank You
Any Questions
28