You are on page 1of 41

Object Oriented Programming

1. Object oriented programming is a new


programming paradigm

2. Other programming paradigms are


- modular programming
- top-down programming
- bottom-up programming
- structured programming
Object Oriented Programming
OOP is an approach to program organization
and development that attempts to eliminate
some of the pitfalls of conventional
programming methods by incorporating the
best of structured programming features with
several powerful new concepts.
Object Oriented Programming
To illustrate some of the major ideas in OOP,
let us consider first how we might go about
handling a real world situation and then we
could make the computer more closely model
the techniques employed
Suppose I wish to send some flowers to my
friend, who lives in a city many miles away.
Picking and carrying the flowers to her door
myself is not possible.
Object Oriented Programming
It may be solved by approaching my local
florist, tell her the details and I can be assured
the flowers will be delivered automatically
The mechanism I used to solve my problem
was to find an appropriate agent (florist) and
to pass her a message containing my request.
It is the responsibility of the florist to satisfy
my request
There is some method or some algorithm
used by the florist to do this
Object Oriented Programming
I do not need to know the method she will use
to satisfy my request
This information is usually hidden from me.
The first principle of object oriented problem
solving is the vehicle by which activities are
initiated
Action is initiated in OOP by the transmission
of a message to an agent (or object)
responsible for the action
Object Oriented Programming
The message encodes the request for an action
and is accompanied by any additional information
(arguments) needed to carry out the request.
The receiver is the agent to whom the message is
sent
If the receiver accepts the message, it accepts the
responsibility to carry out the indicated action
In response to a message, the receiver will
perform some method to satisfy the request
Object Oriented Programming
OOP treats data as a critical element in the
program development and does not allow it to
flow freely around the system
It ties data more closely to the functions that
operate on it, and protects it from accidental
modification from outside functions
OOP allows decomposition of a problem into a
number of entities called objects and then
builds data and functions around these
objects.
Features of OOP
Emphasis is on data rather than procedure
Programs are divided into what are known as objects
Data structures are designed such that they
characterize the objects
Functions that operate on the data of an object are
tied together in the data structure
Data is hidden and cannot be accessed by external
functions
Objects may communicate with each other through
functions
New data and functions can be easily added whenever
necessary
Follows bottom-up approach in program design
Features of OOP
Object 1 Object 2

Communication

Object 3

Organization of data and functions in OOP


Object Oriented Programming
Object Oriented programming as an approach
that provides a way of modularizing programs
by creating partitioned memory area for both
data and functions that can be used as
templates for creating copies of such modules
on demand.
Basic Concepts of OOP
Objects
Classes
Data abstraction and encapsulation
Inheritance
Polymorphism
Dynamic binding
Message Passing
Objects
An object is defined as a logical entity that contains both
data (variables) and code (functions) that manipulate the
data.
Objects are the basic run-time entities in an object
oriented system
Examples : an apple, a place, a person, a chair, etc.,
Objects occupy memory space and have an associated
address like record in Pascal and a Structure in C
During program execution, the communications between
the objects are done by sending messages to one another.
The following figure shows a notation used to represent an
object.

Objects
Representing an object
Classes
A class is defined as a collection of similar objects. i.e objects with
the same data structure and functions are grouped into a class.
Each object is said to be an instance (variables) of the type class.
Once the class has been defined, we can create any number of
objects belonging to that class.
Classes are user-defined data types and behave like the built-in
types of a programming language.
All objects of the particular class occupy equal memory size.
For example chair, tables are members of the class furniture. If
furniture has been defined as a class, then the statement
furniture chair;
will create an object chair belonging to the class furniture.
Each object must be a member of any one class
In class definition, the data are declared and the functions are
defined.
The objects contain the value of the data and the action of the
current function.
DATA ABSTRACTION
Data abstraction is defined as a collection of data and
methods (functions)
Abstraction refers to the act of representing essential
features without including the background details or
explanations.
Data abstraction is the ability to create user defined
data types for modeling real world objects using built-
in data types and a set of permitted operators.
Classes use the concept of abstraction and are defined
as a list is abstract attributes such as size, weight and
cost, and functions to operate on these attributes.
Since the classes use the concept of data abstraction
they are known as Abstract Data Types (ADT).
DATA ENCAPSULATION
The process of combining data and methods in a
single logical unit is called encapsulation
The wrapping up of data and functions (that
operate on the data) into a single unit (called
class)
The only way to access the data is provided by
the functions which are wrapped in the class. The
data is not accessible to the outside world. These
functions provide the interface between the
objects data and program.
It is a technique used to protect the information
in an object from other objects. This concept is
also called as data hiding
INHERITANCE
Inheritance is the process of deriving new classes
from the existing classes. The derived classes
contain all attributes and functions of existing
classes with its own attributes and functions.
It supports the concept of hierarchical
classification.
In OOP, the inheritance principle provides the
idea of reusability.
The existing classes are called base classes and
the inherited classes are called derived classes.
The structure of inheritance is like a tree
structure.
INHERITANCE

From the figure, the class vehicle is divided into subclasses Two wheeler,
Three wheeler and Four wheeler. The principle in this sort of division is that each
sub classes shares common characteristics with the class from which it is derived.

Cars, buses, and motor cycles all have wheels and an engine. These are the
characteristics of vehicles. In addition to these characteristics each sub class also
has its own characteristics.
POLYMORPHISM
Another important OOP concept is polymorphism.
Polymorphism is a Greek term, means the ability for a
message or data to be processed in more than one form
Polymorphism is a technique used to write more than one
function definition with same function name i.e. the same
operation may behave differently on different classes.
For example consider the operation of adding for two
numbers, the operations will generate a sum. If the
operands are strings, then the operations would produce a
third string by concatenation.
Polymorphism is extensively used in implementing
inheritance.
The following figure explains the concept of polymorphism
POLYMORPHISM

In the figure, the function Area is defined in all the classes. But the
operation of the function Area is different.

In the circle class, the Area function calculates the area of circle with the
given radius value. In the square class, the Area function calculates the area of
square with the given side value. The triangle class, the Area function
calculates the area of triangle with the given breadth and height values.
DYNAMIC BINDING
Binding is defined as the connection between the function call and its
corresponding program code to be executed. There are two types of
Binding
- Static Binding
- Dynamic Binding

Static Binding
This type of binding occurs during compile time

Dynamic Binding

The binding occurs during run time. It means that the code associated
with a given procedure call is not known until the time of the call at run-
time. It is associated with polymorphism and Inheritance. This is also
called late binding.
MESSAGE PASSING
An object oriented program consists of a set of objects
that communicate with each other. The process of
programming in an object oriented language, it
involves the following steps.
Creating classes that define objects and their behavior
Creating objects from class definition
Establishing communication between objects
Objects are communicated with one another by
sending and receiving information in the form of
messages.
A message for an object is a request for execution of a
procedure and therefore will invoke a function in the
receiving object that generates the desired result.
MESSAGE PASSING
The general form is

Where
Object_name the name of the object
message name of the function to be executed
information data to be send
Eg. Square.area(10);
Object - Square
message area
information 10

In this example, area is a message sent by the object square to execute the
function area with the data 10.
BENEFITS OF OOPS
The following are the some of the important advantages of object
oriented programming.
Code Reusability
In Object oriented programming, programs and functions that are written
by a user can be reused by other users. With this advantage, we can
eliminate redundant code
Productivity
OOP creates many new data types using basic data types
Code Sharing
In OOP, it allows the programmer to share data and functions which are
common to more than one class by defining it in the parent classes in the
hierarchical order
BENEFITS OF OOPS
Maintenance
Program maintenance is easily done in OOPS because OOP is full of
objects. If particular part of the program needs modification it can be
modified easily.
Data hiding
In OOPS, data hiding helps the programmer to build secure programs that
cannot be invaded by code in other parts of the program
Power
The power is obtained by the special features such as polymorphism,
operator overloading, function overloading and inheritance etc.
Reduced complexity of a problem
In OOP, the given problem is viewed as a collection of different objects.
Each object is responsible to do some specific task. The problem can be
solved by interfacing the objects. It reduces the complexity of the program
design.
BENEFITS OF OOPS
Message passing technique
Message passing technique for communications between
objects makes the interface descriptions with external
systems much simpler
Portable
OOP concepts can be used from micro computer to super
computer. It is not hardware dependent.
Extendability

Object oriented programs can be easily extended from small


to large. It is easy to partition the work in a project based on
objects
APPLICATIONS OF OOP
Research laboratories
Real-time systems
Design Industrial applications
Simulation and Modeling
Requirement Analysis
CIM / CAM / CAD systems
Object oriented databases
Application design programming
Hypertext, Hypermedia and expertext
AI and expert systems
Neural networks and Parallel programming
Decision support systems
Office automation systems
Class
A class is a way to bind the data and its
associated functions together
It allows the data (and functions) to be
hidden, if necessary from external use
When defining a class, we are creating a new
abstract data type that can be treated like any
other built-in data type
Class specification has two parts
1. class declaration
2. class function definitions
Classes and Methods in C++
Class declaration describes the type and scope
of its members
Class function definitions describe how the
class functions are implemented
The general form of a class declaration is
class class_name
{
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
};
Classes and Methods in C++
The class declaration is similar to a struct
declaration
The keyword class specifies that what follows is
an abstract data of type class_name
The class body contains the declaration of
variables and functions
These functions and variables are called class
members
The class members that have been declared as
private can be accessed only from within the class
Public members can be accessed from outside
the class also
Classes and Methods in C++
The variables inside the class are known as
data members and the functions are known as
member functions
Only the member functions can have access to
the private data members and private
functions
However the public members can be accessed
from outside the class
This is illustrated in fig.
Data hiding in classes
CLASS
Private area

Data
No entry to X
Private area Functions

Public area
Data
Entry allowed
Functions
to public area
Classes
A simple class example
class item
{
int number;
float cost;
public:
void getdata (int a, float b);
void putdata (void);
};

The class name item becomes a new type identifier


that can be used to declare instances of that class
type
The class item contains two data members and two
function members
Classes
The data members are private by default while
both the functions are public by declaration
The function getdata() can be used to assign
values to the member variables number and
cost.
The function putdata() for displaying their
values
The data members are usually declared as
private and the member functions as public
Creating Objects
The declaration of a class item does not define
any objects, it specifies what they will contain
Once a class has been declared, we can create
variables of that type by using the class name
For eg. item x;
Creates a variable x of type item. In C++ the class
variables are known as objects. Therefore x is
called an object of type item. We may declare
more than one object in one statement
item x,y,z;
A C++ program with class
#include <iostream> int main()
class item {
{ item x;
int number; cout << \nobject x << \n;
float cost; x.getdata(100,299.95);
public: x.putdata();
void getdata(int a, float b); item y;
void putdata(void) cout << \nobject y << \n;
{ y.getdata(200,175.50);
cout << number : << number << \n; y.putdata();
cout << cost : << cost << \n; return 0;
} }
};
void item :: getdata(int a, float b)
{
number = a;
cost=b;
}
Varieties of classes
Classes in OOP can have several different
forms of responsibility and are used for many
different purposes
The following categories cover a large number
of cases
Data managers
Data sinks or data sources
View or observer classes
Facilitator or helper classes
Varieties of classes
Data managers
It is also called Data, or State, classes, are
classes with the principal responsibility of
maintaining data or state information of one
sort or another
Data manager classes are often recognizable
as the nouns in a problem description and are
usually the fundamental building block of a
design
Varieties of classes
Data Sinks or Data Sources
These are classes that generate data, such as
random number generator, or that accept data
and then process them further, such as a class
performing output to a disk or file.
Unlike a data manager, a data sink or data
source does not hold the data for any period
of time, but generates it on demand (for a
data source) or processes it when called upon
(for a data sink)
Varieties of classes
View or Observer Classes
An essential portion of most applications is
the display of information on an output device
such as terminal screen. Because the code for
performing this activity is often complex,
frequently modified and largely independent
of the actual data being displayed
It is good programming practice to isolate
display behavior in classes other than those
that maintain the data being displayed
Varieties of classes
Facilitator or Helper classes
These are classes that maintain little or no
state information themselves but assist in the
execution of complex tasks.

You might also like