03. oop concepts

36
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 1 Haresh Jaiswal Rising Technologies, Jalna.

Upload: haresh-jaiswal

Post on 06-Aug-2015

135 views

Category:

Education


1 download

TRANSCRIPT

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 1

Haresh Jaiswal

Rising Technologies, Jalna.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 2

Object Oriented Concepts Objects

Classes

Data Abstraction

Encapsulation

Polymorphism

Inheritance

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 3

Object Objects are the basic run-time entities in an OOP

environment.

They may represent:

a person,

a place,

a bank account or

any item that the program has to handle.

Objects are simply variables of type class. Or In pure OOPterms an object is an instance of a user defined data typeclass.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 4

Object As the name object-oriented implies, objects are key to

understand object-oriented technology.

You can look around you & can see many examples ofreal-world objects:

your cell phone,

your dog,

your television set,

your car.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 5

Object All these real-world objects share two characteristics:

they all have state

and they all have behaviour

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 6

Real-World Objects A Real World Object : Dog

have state like

1. Name,

2. Colour,

3. Breed

have behaviours like

1. Barking,

2. Eating

State Value

Name Tommy

Colour Brown

Breed Labrador

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 7

Real-World Objects A Real World Object : Car

have state like

1. Brand,

2. Make,

3. Engine CC

have behaviours like

1. Accelerate,

2. Brake,

3. Change Gear State Value

Brand Maruti

Make Swift

Engine CC 1248 CC

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 8

Programmatic Objects Software/Programmatic objects are modelled after real-

world objects in that they too have state and behaviour.

A Programmatic object : Box

have state like

Height,

Width,

Depth

have behaviours like

getDimensions()

putVolume()

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 9

Programmatic Objects A Programmatic object : Bank Account

have state like

Acct No,

Acct Holder’s Name,

Current Balance

have behaviours like

Deposit(),

Withdrawal()

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 10

Programmatic Objects A Programmatic object : Employee

have state like

Employee ID,

Employee Name,

Basic Salary

have behaviours like

getData(),

putSalary()

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 11

Programmatic Objects Everything that the software object knows (its state) and

can do (its behaviour) is expressed by the variables andmethods within that object.

A software/programmatic object maintains:

its state in variables

implements its behaviour with methods/functions.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 12

Programming Objects

Object : Student

State (Data)

RollNo

Name

M1, M2, M3

Behaviour (Functions)

getData()

putResult()

Object : Employee

State (Data)

EmpNo

Name

Basic, Ta, Da, Gross

Object : Box

State (Data)

Height

Width

Depth

Behaviour (Functions)

getDimensions()

putVolume()

Behaviour (Functions)

getData()

putSalary()

Each object contains Data and Code to manipulate that Data

Or

An object is a bundle of variables and related methods/functions.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 13

Example of Objects A software object that modelled your real-world box

would have variables that indicates the box’s currentstate:

Height : 4

Width : 7

Depth : 6

The software object box would also havemethods/functions to:

getDimensions() : to take dimensions as input from user

putVolume() : to calculate and print volume of box

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 14

Example of Objects A software object that modelled your real-world car

would have variables that indicates the car’s currentstate:

speed : 10 mph,

pedal cadence : 90 rpm,

current gear is : 5th.

The software object car would also havemethods/functions to:

brake,

change the pedal cadence,

change gears.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 15

Example of Objects You can think of an object as a fancy variable;

it stores data and you can “make requests” to that object, asking it to perform operations on itself.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 16

Example of Objects You can represent real-world objects using software

objects. You can represent:

real-world dog as software object in an animation program.

real-world car as a software object within a game program.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 17

Class Classes are actually special kinds of templates from which

you can create objects.

Definition : Class is a way of binding Data and itsassociated Functions/Methods together.

When you define a class, you define a blueprint for anabstract data type.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 18

Class A class is created to characterise your programmatic

object.

Everything a programmatic object knows (its state) andcan do (its behaviour) is characterised using variables andmethods defined inside the class.

A class is a logical abstract but an object has a physicalexistence in the memory.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 19

Abstract Data Type Defining a Class means creating an abstract data type in

any object oriented programming language.

A data type can be called abstract, if it do not reveal itsinternal implementation to the user of that type.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 20

Abstract Data Type There are 2 parts to each ADT:

The external (public) part, which consists of:

the conceptual picture (user's view of how the object looks like)

the conceptual operations (what the user can do to the ADT)

The internal (private) part, which consists of:

the representation (how the structure is actually stored)

the implementation of the operations (the actual code)

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 21

Abstraction In simple words, abstraction is ‘Hiding implementation

details behind the interface’

When abstraction is created our concern is limited towhat it can do rather how it is doing it.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 22

Abstraction When we drive a car we often need to change the gears

of the vehicle but we are not concerned about the innerdetails of the vehicle engine. What matters to us is thatwe must shift a gear, that's it. This is abstraction

Show only those details which matters to the user.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 23

Abstraction What exactly is Hiding implementation details behind theinterface?

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 24

Abstraction

class box

{

private:

float h, w, d;

public:

void getDimensions()

{

cout << “Enter Height, Width & Depth : “;

cin >> h >> w >> d;

}

void putVolume()

{

cout << “Volume : “ << (h * w * d);

}

};

Class Definition, the internal details/implementation of ADT

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 25

Abstraction

Object : Box

State(Data)

Height

Width

Depth

Behaviour/Functions

getDimensions()

putVolume()

An Object of Class Box, user has knowledge about its external

interface only.

Actual implementation,

User has no knowledge

about this part of object.

External Interface to use

this object,

User has knowledge only

about this part of the object.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 26

Encapsulation The wrapping of code and data into a single unit is called

as encapsulation.

Encapsulation binds together data and code tomanipulate that data, it keeps both them safe fromoutside interference and mis-use.

Encapsulation is a protective container that prevents codeand data from being accessed by other code definedoutside the container.

Creating a Class is the way to implement Encapsulation.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 27

Encapsulation Object encapsulates data and operations in one unit

An object is a bundle of data; who know how to do thingsto itself.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 28

Polymorphism In object-oriented programming polymorphism means

the ability to appear in many forms.

polymorphism refers to a programming language's abilityto process objects differently depending on their datatype or class.

It is the provision of a single interface to entities ofdifferent types.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 29

Polymorphism An operation may perform different behaviors in different

instances and the behavior depends upon the type ofdata used in the operation.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 30

Polymorphism Consider the operation of adding two integer values using

+ operator, then the operation will generate a sum, but ifoperands in the operation is of string type then theoperation would produce a third string by concatenation.

5 + 2 => 7 // integer operands

“Rising” + “Tech” => “Rising Tech” // string operands

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 31

Polymorphism So this process of making an operator to perform

different behaviors in different instances is known as“operator overloading”

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 32

PolymorphismClass : Shape

---------------------------

Draw()

Object : Circle

---------------------

Draw(circle)

Object : Box

---------------------

Draw(box)

Object : Triangle

---------------------

Draw(triangle)

This figure illustrates that a single function name can be used to handle

different number and different type of arguments.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 33

Polymorphism Using a single function name to perform different types of

tasks is known as ‘Function Overloading’

This is something similar to a particular word havingseveral different meanings depending on the context.

I Saw a Saw with a Saw Sawing a Saw...

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 34

Virtual Functions Virtual functions is the ability to redefine methods inderived classes.

For example, given a base class shape, polymorphismenables the programmer to define different areamethods for any number of derived classes, such ascircles, rectangles and triangles.

No matter what shape an object is, applyingthe area method to it will return the correct results.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 35

Polymorphism

Polymorphism

Compile Time

Function Overloading

Operator Overloading

Runtime

Virtual Functions

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 36

Polymorphism Polymorphism plays an important role in allowing objects

to have different internal structures but to share thesame external interface.