welcome to object oriented programming. introduction structured programming was most common way to...
TRANSCRIPT
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
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
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
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 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
OBJECT BASED PROGRAMMINGSubset of object oriented programmingImplement some feature of OOP like
information hiding, abstraction, classes, function overloading but not implement inheritance and polymorphism
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
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
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
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
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
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
Mapping an Abstraction into softwareReal world Abstraction software
{data,data…}
{method,method}
entity
attributes
behaviour
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
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
A Class is define asClass <class name> { private: // hidden members/methods protected: //unimportant implementation details public : // exposed important details
};
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}
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
Abstract class and Concrete classExample
Abstract class
Shape
Concrete classCircle class
Concrete classRecantagle class
Concrete classTriangle class
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
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 OverloadingFunction 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 signatureExample void squar (int a, float b); //function 1Void squar (int x, float y);// same signature
as function 1
Declaration and DefinitionDifferent signatures void prnsqr ( int i); void prnsqr ( char c); void prnsqr ( float f); void prnsqr ( double d);
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
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
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 ?
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;}
Structural of procedural programmingMain function
Function Function Function
function Function
Function Function Function
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
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*.
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
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
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
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
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