You are on page 1of 32

Rohama Javed

Lecturer
Wah Engineering College
1. “The Object Oriented Programming in C++”
by Robert Lafore, SAMS, Fourth Edition

2. “C How to Program”, Aikman Series


 1st Generation Languages
Machine Language
 2nd Generation Languages
Assembly Language

 3rd Generation Languages


Procedural Programming Languages like Basic,
Pascal, Fortran, C etc.

 4th Generation Languages


OOP e.g. Java ,C++, VB.
 Various programming languages
 Some understandable directly by computers
 Others require “translation” steps
◦ Machine language
 Natural language of a particular computer
 Consists of strings of numbers(1s, 0s)
 Instruct computer to perform elementary
operations one at a time
 Machine dependant
 Assembly Language

◦ English like abbreviations

◦ Translators programs called “Assemblers” to


convert assembly language programs to machine
language.

◦ E.g. add overtime to base pay and store result in


gross pay

LOAD BASEPAY

ADD OVERPAY

STORE GROSSPAY
 High-level languages
◦ To speed up programming even further
◦ Single statements for accomplishing substantial
tasks
◦ Translator programs called “Compilers” to
convert high-level programs into machine
language

◦ E.g. add overtime to base pay and store result in


gross pay
grossPay = basePay + overtimePay
Procedural Language: Object Oriented Language:

 View a program as a  Views a program as a


series of steps to be group of objects that
carried out have certain properties
 Each statement in the and can perform
language tells the certain functions
computer to do  E.g. C++, Java, C#
something
 E.g. C, FORTRAN, Pascal
 Problem:
◦ Cannot cope with very large
project sizes
Procedural Language: Object Oriented Language:

1. Gather ingredients  Model for an Object


Flour, butter, egg, sugar
etc  Properties
2. preheat oven to 350 ◦ List of ingridients, or set
degree of data
3. beat eggs and butter
 Methods
4. add sugar
◦ List of actions or
5. mix instructions
6. bake 10 mins
 Baker
{
◦ Properties (ingredients)
 Flour
 Butter
 Eggs
 Milk
 Cake pan
 Oven
1. Gather ingredients
◦ Methods (actions) Flour, butter, egg, sugar etc
 Bake cookies 2. preheat oven to 350 degree
 Bake cake 3. beat eggs and butter
 Bake pie 4. add sugar
5. mix
} 6. bake 10 mins
 C
 A language written by Brian Kernighan
and Dennis Ritchie.
 This was to be the language that UNIX
was written in to become the first
"portable" language

In recent years C has been used as a general-


purpose language because of its popularity with
programmers.
 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.

 Every thing is object i.e.:


◦ 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, …
 Set of instructions can be referred to as a
subroutine, a subprogram, a procedure or a
function
 Dividing a program into functions and modules
 Problems with Procedural Programming

– large programs become excessively object


complex
– functions have unrestricted access to Data
global data.
Member
– unrelated functions and data, the basis Function
of the procedural paradigm, provide a Member
Function
poor model of the real world
 Two kinds of data in Procedural program:
 Local data:
◦ Hidden inside a function
◦ Used exclusively by the function
◦ Closely related to its function and is safe from
modification by other functions.

 Global Data:
◦ 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
Global Global Global
Data Data Data

Function Function Function Function

 In a large program, there are many functions and


many global data items.
◦ This leads to an even larger number of potential
connections between functions and data which causes
problems
1. It makes a program’s structure difficult to conceptualize.
2. Makes the program difficult to modify.
3. A change made in a global data item may necessitate
rewriting all the functions that access that item.
 Another problem with the procedural
paradigm is that the arrangement of separate
data and functions does a poor job of
modeling things in the real world.
 In the physical world we deal with objects
such as people and cars.
Such objects aren’t like data
and they aren’t like functions.

 Complex real-world objects have both


attributes and behavior.
 Attributes:
◦ Also called characteristics
For People, the attributes are eye color,
gender and job title etc…

For cars, the attributes are horsepower and


number of doors etc…

◦ Have a certain specific values, such as blue (for eye


color)
 Behavior
◦ A real-world object does in response to some
stimulus
 The fundamental idea is to combine into a
single unit both data and functions that
operate on the data.
 Such a unit is called an “Object”.
 An object’s functions are called “member
functions” in C++
 And its data is called “ data members”.
 An object’s data is typically accessed through
its member functions, i.e. it is hidden from
accidental alteration
 Data and its function are said to be
encapsulated into a single entity
 Data encapsulation and data hiding are key
elements of object-oriented languages
 If you want to modify data in an object, you
know exactly what functions interact with it
(i.e. the member functions of the object).
 This simplifies writing, debugging, and
maintaining the programs
 An OO program consists of a number of
objects which communicate with each other’s
member functions
object

Data

Member
Function

Member
object
Function
object

Data
Data
Member
Member Function
Function
Member
Member Function
Function
 Objects
 Classes

 Encapsulation
 Inheritance
 Reusability
 Polymorphism and overloading
 Declaring a class doesn’t create any objects,
just as mere existence of data type int
doesn’t create any variables.

 A class is thus a description of a no. of


similar objects.

 For instance, HUMAN is a class, and JOHN is


 Objects belong to classes
 A class and an object of that class has the
same relationship as a data type and a
variable
 All objects with the same characteristics (data
and functions) constitute one class.
 A class serves only as a plan, or a template,
or sketch- of a number of similar things
 Information hiding
 Encapsulation is the mechanism that binds
together code and the data it manipulates,
and keep both safe from outside inteference
object
and missuse
object

Data
Data
Member
Member Function
Function
Member
Member Function
Function
 Derive other (sub-)classes from an existing
class
 The original class is called the BASE CLASS;
the others are DERIVED CLASSES
 Each class shares common characteristics
with the class from which it was derived, and
can also add its own modifications, additions.
 For instance, VEHICLE is a class from which
Base class

F A
Feature

F B
Feature

F A
Feature
F A
Feature F A
Feature
F B
Feature
F B
Feature F B
Feature
F
Feature D

F C
Feature F F
Feature
F
Feature E

Derived classes
 One class can be distributed to other
programmers for use in their own programs
 In OOP, the concept of inheritance leads to
the idea of Reusability
 A programmer can take an existing class and,
without modifying it, add additional features
and capabilities to it
 Using operators or functions in different ways
depending on what they are operating on is
called polymorphism (lit. one thing with several
distinct forms)
 Overloading is a special case of polymorphism,
e.g. +, -, /, << etc.

You might also like