01.2 object oriented programming

40
Welcome to Welcome to OBJECT ORIENTED PROGRAMMING

Upload: mamata-parab

Post on 18-Feb-2016

228 views

Category:

Documents


1 download

DESCRIPTION

uhuhuhuhuh

TRANSCRIPT

Page 1: 01.2 Object Oriented Programming

Welcome to Welcome to OBJECT ORIENTED PROGRAMMING

Page 2: 01.2 Object Oriented Programming

INTRODUCTIONStructured programming was most common

way to organize a program.A programming paradigm defines the

methodology of designing and implementing program using the key features and building blocks of a programming language

Page 3: 01.2 Object Oriented Programming

PROCEDURAL PROGRAMMINGLays more emphasis on procedure than dataSeparates the function and data manipulated

by themWhenever the definition of a type changes,

the functions referring to this type must also be changed to reflect the change

Page 4: 01.2 Object Oriented Programming

PROCEDURAL PROGRAMMINGThe change in the design must also be

modified to copy with the change in structure that shows procedural programming is

susceptible to design changesAs design changes lead to many modification

in the code this lead to increased time and cost overheads at times

Page 5: 01.2 Object Oriented Programming

OBJECT BASED PROGRAMMING In object based programming data and its

associated meaningful function are enclosed in one single entity a class

Class are allowed to access its interface (how the uses views) but cannot access its implementation details (how the process is actually taking place)

Page 6: 01.2 Object Oriented Programming

OBJECT BASED PROGRAMMINGWhenever any change in the definition of

type user’s interface remains unaffected generally

The user cannot access the data of the class directly which is possible in procedural programming

the change is localized to the definition of the change function

Page 7: 01.2 Object Oriented Programming

OBJECT BASED PROGRAMMINGSubset of object oriented programmingImplement some feature of OOP like

information hiding, abstraction, classes, function overloading but not implement inheritance and polymorphism

Page 8: 01.2 Object Oriented Programming

OBJECT BASED PROGRAMMINGAdvantages :overcome most shortcoming of procedural

programming it localizes the changes and hide implementation details from userIt support user defined typesImplement information hiding and

abstraction

Page 9: 01.2 Object Oriented Programming

OBJECT BASED PROGRAMMINGLimitation One major limitation is ability to represent

real world relationships that exist among object for example both car and truck are vehicles this cannot be represented in OBP as it not support inheritance

Page 10: 01.2 Object Oriented Programming

OBJECT ORIENTED PROGRAMMINGSuperset of OBPAll the features of OBP and overcome its

limitation by implementing inheritance so that real world relation among object can be represented programmatically

Page 11: 01.2 Object Oriented Programming

OBJECT ORIENTED PROGRAMMING OOP Concepts are data abstraction, data

hiding, data encapsulation, classes, objects, inheritance and polymorphism.

Implementing OOP Concepts in C++1. Approach for writing software in which

data and behavior are packed together 2. An abstraction is a named collection of

attributes and behavior relevant to modeling a given entity

Page 12: 01.2 Object Oriented Programming

OBJECT ORIENTED PROGRAMMINGA class is a named software representation

for an abstractionAn object is a distinct instance of a classSoftware code in OOPs is written to define

classes, objects and manipulate these object

Page 13: 01.2 Object Oriented Programming

Implementing objects Real World objects have physical characteristics

(state) and behavior e.g., motorbikeCharacteristics – current gear, two wheel etcBehavior – braking, accelerating etc Their state is maintained through variables or data

itemsTheir behaviour is implemented through function

generally called methods

Page 14: 01.2 Object Oriented Programming

Mapping an Abstraction into softwareReal world Abstraction software

{data,data…}

{method,method}

entity

attributes

behaviour

Page 15: 01.2 Object Oriented Programming

Implementing Data hiding, Data Abstraction and EncapsulationEncapsulation is wrapping up of

characteristics and behaviour into one unitA class bind together data and its associated

functions under one unit thereby enforcing encapsulation

Abstraction means representation of essential features without including the background details or explanation

Page 16: 01.2 Object Oriented Programming

Implementing EncapsulationAnything that an object does not know or

cannot do is excluded from the objectEncapsulation is used to hide unimportant

implementation details from the objectsPacking an object’s variables within the

protective custody of its methods is encapsulation and this task is accomplished through classes

Page 17: 01.2 Object Oriented Programming

General structure of a class

{data,data,….}

{method,method,…}

Private

Protected

Public

Page 18: 01.2 Object Oriented Programming

A Class is define asClass <class name> { private: // hidden members/methods protected: //unimportant implementation details public : // exposed important details

};

Page 19: 01.2 Object Oriented Programming

Implementing InheritanceInheritance is implement in C++ specifying the name of the (base) class from which the class being defined (the derived class) has to inherit from it.Class <derived class name>:<base class name>{ derived class own features}

Page 20: 01.2 Object Oriented Programming

Abstract class and Concrete classWhich defines an interface but does not provide

implementation for all its member functionAn abstract class is meant to be used as the base

class from which other classes are derivedThe derived class is expected to provide

implementations for the member functions that are not implemented in the base class

A derived class that implements all the missing functionality is called a concrete class

A concrete class drives from abstract class

Page 21: 01.2 Object Oriented Programming

Abstract class and Concrete classExample

Abstract class

Shape

Concrete classCircle class

Concrete classRecantagle class

Concrete classTriangle class

Page 22: 01.2 Object Oriented Programming

Implementing polymorphismPolymorphism is the attribute that allows one

interface to be used with different situationC++ implement polymorphism through virtual

functions, overloaded functions and overloaded operators.

A virtual function is used to specify the interface in abstract class but its implementation details are made available in concrete class (es)

Overloading means a name having two or more distinct meaning

Page 23: 01.2 Object Oriented Programming

Function Overloading A function with same name having several

definitions that are differentiable by the number or types of their arguments is known as overloaded function and this process is known as function overloading

Example float sum (int a, int b); float sum (float x,float y);

Page 24: 01.2 Object Oriented Programming

Function OverloadingFunction overloading not only implement

polymorphism but also reduce number of comparison in a program

Push(int) Push(float) Pop(int) Pop(float)

Page 25: 01.2 Object Oriented Programming

Declaration and Definition A function argument list is known as

function’s signatureExample void squar (int a, float b); //function 1Void squar (int x, float y);// same signature

as function 1

Page 26: 01.2 Object Oriented Programming

Declaration and DefinitionDifferent signatures void prnsqr ( int i); void prnsqr ( char c); void prnsqr ( float f); void prnsqr ( double d);

Page 27: 01.2 Object Oriented Programming

Declaration and DefinitionWhen a function name is declare more than once in a

program the compiler will interpret the second (and subsequent) declaration (s) as follows :

1) If the signatures of subsequent functions match the previous function’s then the second is treated as a re-declaration of the first

2) If the signature of the two functions match exactly but the return type differ,the second declaration is treated as an erroneous re-declaration of the first and is flagged at compile ttime as an error for example;

flaot square (float f); double square (float x); // error

Page 28: 01.2 Object Oriented Programming

Declaration and DefinitionFunction with same name and same signature but

different return type are not allowed in C++You can have different return types, but only if

the signature are also different: float square (float f);// different signature double square (double d); // allowedIf the signature of the function differ in either the

number or type of their arguments, the two function are considered to be overloaded

Page 29: 01.2 Object Oriented Programming

Questions OOPQ1 write briefly about different programming

paradigms ?Q2 Discuss major OOP concepts briefly?Q3 what is the signifance of private, protected

public specifiers in a class?Q4 Discuss the relation between abstract and

concrete classes ?Q5 How polymorphism implemented in c++ ?Q6 How does the complier interpret more than

one definitions having same name ? What steps does it follow to distinguish these ?

Page 30: 01.2 Object Oriented Programming

Questions OOP Q7 what will be the output of following program?#include<iostream.h>Int area (int s){Return (s*s);}Float area (int b,int h){Return (o.5 * b* h);}Int main ( ){Cout <<area(5)<<endl;Cout<<area(4,3)<<endl;Cout<<area(6,area(3))<<endl;Return 0;}

Page 31: 01.2 Object Oriented Programming

0rganization of data and functions in OOP object object

data

function

data

function

data

function

Page 32: 01.2 Object Oriented Programming

Structural of procedural programmingMain function

Function Function Function

function Function

Function Function Function

Page 33: 01.2 Object Oriented Programming

Restrictions on Overloaded FunctionsAny two functions in a set of overloaded functions

must have different argument listOverloading functions with argument lists of the

same types, based on return type alone is an error

Member function cannot be overloaded soley on the basis of one being static and other non static

Typedef declarations do not define new types: they introduce synonyms for existing types,they do not affect the overloading mechanism

Page 34: 01.2 Object Oriented Programming

Restrictions on Overloaded FunctionsExample typedef char * PSTR void Print(char *szToPrint); void Print(PSTR szToPrint);The preceding two function have identical lists.

PSTR is synonym for char*.

Page 35: 01.2 Object Oriented Programming

Calling overloaded functionsA function call first matches the prototypes

available with number and type of arguments provided with the function call and then call the appropriate function for execution

Page 36: 01.2 Object Oriented Programming

Step involved in Finding the best matchA match: A match is found for the function

callNo match: No match is found for a function

callAmbiguous match: More than one defined

match for the function call

Page 37: 01.2 Object Oriented Programming

Step involved in Finding the best matchSearch for exact match :if the type of the actual

argument exactly matches the type of one defined instance

A match through promotion : if no exact match is found an attempt is made to achieve a match through of the actual argument

A match through application of standard C++ conversion rules : if no match or through a promotion is found an attempt is made to achieve a match through a standard conversion of the actual argument

Page 38: 01.2 Object Oriented Programming

Step involved in Finding the best matchA match through application of a user-defined

conversion: if all the above mentioned step fail, then the compiler will try the user-defined conversions in combination with integral promotions and built-in conversion to find a unique match

Page 39: 01.2 Object Oriented Programming

Advantages of OOPRe-use of code : linking of code to objects and explicit

specification of relations between objects allows related objects to share code

Ease of comprehension: the classes can be set up to closely represent the generic application concepts and process

Ease of fabrication and maintenance : the concepts such as encapsulation, data abstraction allow for very clear designs when an object is going into disallowed states, which are not permitted only its methods needs to investigated

Easy redesign and extension : the same concepts facilitate easy redesign and extension

Page 40: 01.2 Object Oriented Programming