OBJECT ORIENTED PROGRAMMING

Download Report

Transcript OBJECT ORIENTED PROGRAMMING

Welcome to
OBJECT ORIENTED PROGRAMMING
INTRODUCTION
 Structured 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
PROCEDURAL PROGRAMMING
 Lays more emphasis on procedure than data
 Separates the function and data manipulated by them
 Whenever the definition of a type changes, the
functions referring to this type must also be changed
to reflect the change
PROCEDURAL PROGRAMMING
 The change in the design must also be modified to
copy with the change in structure
that shows 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
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)
OBJECT BASED PROGRAMMING
 Whenever 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
OBJECT BASED PROGRAMMING
 Subset of object oriented programming
 Implement some feature of OOP like information
hiding, abstraction, classes, function overloading but
not implement inheritance and polymorphism
OBJECT BASED PROGRAMMING
 Advantages :
overcome most shortcoming of procedural
programming
it localizes the changes and hide
implementation details from user
It support user defined types
Implement information hiding and abstraction
OBJECT BASED PROGRAMMING
 Limitation
 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
OBJECT ORIENTED PROGRAMMING
 Superset of OBP
 All the features of OBP and overcome its limitation by
implementing inheritance so that real world relation
among object can be represented programmatically
OBJECT ORIENTED PROGRAMMING


1.
2.
OOP Concepts are data abstraction, data hiding,
data encapsulation, classes, objects, inheritance and
polymorphism.
Implementing OOP Concepts in C++
Approach for writing software in which data and
behavior are packed together
An abstraction is a named collection of attributes
and behavior relevant to modeling a given entity
OBJECT ORIENTED PROGRAMMING
 A class is a named software representation for an
abstraction
 An object is a distinct instance of a class
 Software code in OOPs is written to define classes,
objects and manipulate these object
Implementing objects
 Real World objects have physical characteristics (state) and
behavior e.g., motorbike
Characteristics – current gear, two wheel etc
Behavior – braking, accelerating etc
Their state is maintained through variables or data items
Their behaviour is implemented through function generally
called methods
Mapping
Real
world
an Abstraction
Abstractioninto software
software
{data,data…}
attributes
entity
{method,method}
behaviour
Implementing Data hiding, Data Abstraction and
Encapsulation
 Encapsulation is wrapping up of characteristics and
behaviour into one unit
 A 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
Implementing Encapsulation
 Anything that an object does not know or cannot do is
excluded from the object
 Encapsulation is used to hide unimportant
implementation details from the objects
 Packing an object’s variables within the protective
custody of its methods is encapsulation and this task is
accomplished through classes
General structure of a class
{data,data,….}
Private
{method,method,…}
Protected
Public
A Class is define as
Class <class name>
{ private:
// hidden members/methods
protected:
//unimportant implementation details
public :
// exposed important details
};
Implementing Inheritance
Inheritance 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
}
Abstract class and Concrete class
 Which defines an interface but does not provide




implementation for all its member function
An abstract class is meant to be used as the base class from
which other classes are derived
The 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
Abstract class and Concrete class
Example
Concrete class
Circle class
Abstract class
Shape
Concrete class
Recantagle class
Concrete class
Triangle class
Implementing polymorphism
 Polymorphism is the attribute that allows one interface to
be used with different situation
 C++ 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
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);
Function
Overloading
 Function overloading not only implement
polymorphism but also reduce number of comparison
in a program
Push(int)
Push(float)
Pop(int)
Pop(float)
Declaration and Definition
 A function argument list is known as function’s
signature
Example
void squar (int a, float b); //function 1
Void squar (int x, float y);// same signature
as function 1
Declaration and Definition
 Different signatures
void prnsqr ( int i);
void prnsqr ( char c);
void prnsqr ( float f);
void prnsqr ( double d);
Declaration and Definition
 When 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
Declaration and Definition
 Function 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); // allowed
 If the signature of the function differ in either the number
or type of their arguments, the two function are considered
to be overloaded
Questions OOP
 Q1 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 ?
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;
}
0rganization of data and functions in OOP
object
object
data
data
function
function
data
function
Structural of procedural programming
Main function
Function
Function
Function
function
Function
Function
Function
Function
Restrictions on Overloaded Functions
 Any two functions in a set of overloaded functions must
have different argument list
 Overloading 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
Restrictions on Overloaded Functions
 Example typedef char * PSTR
void Print(char *szToPrint);
void Print(PSTR szToPrint);
The preceding two function have identical lists. PSTR is
synonym for char*.
Calling overloaded functions
 A 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
Step involved in Finding the best match
 A match: A match is found for the function call
 No match: No match is found for a function call
 Ambiguous match: More than one defined match for
the function call
Step involved in Finding the best match
 Search 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
Step involved in Finding the best match
 A 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
Advantages of OOP
Re-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