Object Oriented Programming

10

Click here to load reader

Transcript of Object Oriented Programming

Page 1: Object Oriented Programming

OBJECT ORIENTED

PROGRAMMING

Page 2: Object Oriented Programming

PROCEDURAL PROGRAMMING

The problem is decomposed in smaller units

named procedures and data can be

assembled in packages, called structures.

Data and procedures are separated.

C, Pascal, Fortran are procedural languages

Limitations of Procedural Programming

Procedural programming is susceptible to design

changes

As design changes lead to many modification in the

code this lead to increased time and cost overheads

at times

Page 3: Object Oriented Programming

OBJECT ORIENTED PROGRAMMING

Programs created in procedural languages are difficult to manage, hard to maintain and expensive to extend.

A new way to program : Object Oriented Programming (OOP)

Object oriented programming is a tool for new challenges in software development

offers a closer fit to the way human think

improves communication

improves the quality of software

Goal : to design high quality software at low cost

Important OOP concepts: Classes

Objects

Encapsulation

Data hiding

Inheritance

Polymorphism

Page 4: Object Oriented Programming

OBJECTS

Don’t think about data and functions

separately, think about objects. Objects are

small bundles of data which know how to do

thing with themselves.

Example:

You can think: A car is a collection of wheels, doors,

seats, windows. But think what a car can do:

move, speed up, slow down, stop, park, etc. Put

everything you know about a car in one object.

An Object is an identifiable entity with some

characteristics and behavior.

Page 5: Object Oriented Programming

CLASSES

All cars are objects of the same type. The description

of this type is a class: class Car

It is a template representing a group of objects that

share a common properties and relationships.

Software code in OOPs is written to define classes,

objects and manipulate these object

Real World objects have physical characteristics

(state) and behavior e.g., motorbike

Characteristics – current gear, two wheel etc

Behavior – braking, accelerating etc

Their characteristics is maintained through variables

or data items.

Their behavior is implemented through function

generally called methods

Page 6: Object Oriented Programming

Data Abstraction

It refers to the act of representing essential features withoutincluding the background details or explanations.

Encapsulation

The wrapping up of data and functions into a single unit (class) iscalled as encapsulation. A class binds together data and itsassociated functions under one unit thereby enforcingencapsulation. It is a way of implementing abstraction.

Benefits with encapsulation: (i) Modularity. (ii) Informationhiding.

Data Hiding:

A class groups its members into three sections: private, protectedand public. The private and protected members remain hiddenfrom outside world. Thus through private and protectedmembers, a class enforces data – hiding. (The outside world isgiven only the essential and necessary information throughpublic members, rest of the things remain hidden, which isnothing but abstraction.)

Page 7: Object Oriented Programming

class ABC

{ private:

int a,b;

protected:

int c,d;

public:

int e,f;

void disp( )

{ }

};

In the above class public members(ie e,f and disp( ))only will be available to outside the class..

The other private members (a,b), protected members(c,d) will not be available to outside the class. Thisconcept is called data hiding.

Page 8: Object Oriented Programming

INHERITANCE

The capability of one class to inheritproperties from another class is called asinheritance. The class inheritance, letsyou generate a model that is closer to thereal world. The class whose propertiesare inherited is called base class (Superclass) and the class that inherits theproperties is known as derived class(Base class).

The most important advantage of inheritance is code reusability.

Page 9: Object Oriented Programming

For example, a student as well as a staff is a

Person. Both have some common properties.

Inheritance allows the programmer to reuse

defined properties. A subclass defines only those

features that are unique to it .

Page 10: Object Oriented Programming

POLYMORPHISM

Polymorphism is a Greek word which can be

divided into 2 sub word i.e., poly stands for

many and morphism stands for forms.

Polymorphism means ability to take more than

one form.

Polymorphism in the context of object-

oriented programming, is the ability to create

a variable, a function, or an object that has

more than one form.

Polymorphism allows the usage of same

operators or functions in different ways. eg:

7+3 is used for addition "poly"+"morphism" is

used for concatenating these two words