SlideShare a Scribd company logo
1 of 25
POLYMORPHISM
Definition…….
• Polymorphism is a Greek term that means ability to
exist in more than one form. For example, an English
word “bat” can have two meanings in two different
contexts., i.e. a cricket bat and an animal bat.
• Similarly, in Object oriented system, polymorphism can
be in functions as well as in existing operators( with
few exceptions). In broader sense, Polymorphism is of
two types:
• Static Polymorphism
• Dynamic Polymorphism
Static Polymorphism
• Also known as early binding or compile time
polymorphism it’s the ability to bind the
function codes with the function calls
(provided all the functions have same name)
at the compilation time. To implement static
polymorphism, we have the concept of
function overloading & operator overloading.
Dynamic Polymorphism-
• Also known as late binding or run time
polymorphism, it is the ability to bind the
function codes with the function calls
(provided all the functions have same name)
at the program run time. To implement
dynamic polymorphism, we have the concept
of virtual functions.
Static Polymorphism
Function overloading: It’s a phenomenon for
a function with same name to exist with
different number and types of arguments. Eg.
a function called area() can be with two
arguments for finding area of a rectangle and
the same function area() can be with one
argument for finding area of a circle. The
syntax for both the functions will be:
Syntax:
void area( int l, int b)
{
cout<<”area of rectangle”<< l*b;
}
void area(int r)
{
cout <<” area of circle”<< 3.14*r*r;
}
Explanation….
• When the compiler will come across these
two function calls in main(), it will bind the
function call to area() with two arguments to
first function and function call to area() with
one argument to the second function. Hence,
this is called static binding of function call
with function code that is done at compile
time.
Operator overloading:
• In operator overloading, one operator is
capable of existing in more than one forms
without changing the actual meaning of the
operator. Eg. a “+” operator can be used to
add two numbers as well as can be used to
concatenate two strings or can add two
objects of any class. But in any case it will just
add. Any other operation is invalid.
Exceptions….
Except for some operators, all the operators can be
overloaded.
These exceptions are---
. (Member access or dot operator)
.*(Pointer to member operator)
: :(scope resolution operator)
sizeof( ) operator
?: (ternary operator)
Syntax for defining operator function
The operator can be overloaded through
operator function. Its syntax is:
return type class-name : : operator op (argument list)
{
Function body;
}
Here, the operator function is a member function
of any class so it has to be associated with a class
when defined outside the class. “operator” is the
keyword and “op” is the operator that has to be
overloaded.
An example…..
class xyz
{
int a, b;
public:
void getdata( )
{
cin>>a>>b;
}
void operator –( );
void putdata( )
{
cout <<a<<b;
}
};
void xyz : : operator –( )
{
a = -a;
b = -b;
}
int main( )
{
xyz ob; ob.getdata( );
-ob; // calling the operator
function
ob.putdata( );
return 0;
}
• In the previous slide, the example of
overloading unary minus operator was
shown.
• Above operator function can also be used
with friend function. It is shown as: friend
void operator (xyz &ob);
• Here “&” is the reference operator and it
refers to the object ob of the class xyz.
Pointers to function
• Function pointers are widely used in dynamic
binding.
• The concept of pointer to function acts as a
base for pointers to the members(.*).
• They are used to select a function dynamically
at run time.
• General syntax
• Datatype (*function_name)();
An example
• typedef void (*funptr)
(int,int);
• void add(int i, int j)
• { cout<< i<<"+"
<<j<<"="<<i+j;}
• void subtract( int i, int j)
• {
• cout<< i<<"-"
<<j<<"="<<i-j;
• }
• int main() {
• funptr ptr;
• ptr = &add;
• ptr(1,2);
• cout<<"n";
• ptr =&subtract;
• ptr(3,2);
•
• return 0;
• }
Pointer to object
• Void main()
• {
• Item x,*ptr;
• ptr= &x;
• x.getdata();//ptr-> getdata();
• x.show();// ptr->show();//(*ptr).show()
• }
• The parentheses are necessary because the dot
operators has higher precedence than the
indirection operator *.
This Pointer
A pointer to the calling object is referred to as
this pointer. It is passed as a default argument
to any member function that is called by an
object.
But, we can also refer to the calling object
explicitly in a member function by using this
pointer. Eg.
An example
• class sample
• {
• int i;
• public :
• void getdata( )
• {
• cin>> this -> i;
• }
• void putdata( )
• {
• cout << this ->i;
•
• }
• };
Use of pointer to object
• class item {
• int code;
• float price;
• public:
• void getdata()
• { cout<<"n enter code and price of the item:";
cin>>code>>price;}
• void show()
• { cout<<"n Code:"<<code; cout<<"nPrice:"<<
price;}};int main() { item X, *ptr; ptr= &X; ptr-
>getdata(); ptr->show(); return 0;}
Pointer to the object of base class.
• We always create the pointer to the object of
base class.
• Pointer to the object of base class are type
compatible with pointers to object of a
derived class. i.e a single pointer variable can
be made to point to object belonging to
different class.
Virtual function
• Virtual functions are those which are
preceded by a keyword virtual.
• They help in deciding which function has to be
called at run time depending upon the type of
function pointed by the base pointer.
Pure virtual functions
• Virtual functions with no definition and which are
equated to a zero are called pure virtual
functions. They are a basic feature of an Abstract
class.
• Virtual function defined in a base class is seldom
used for performing any task. They servers as a
place holders.
• Such functions are called do nothing functions or
pure virtual functions.
• Ex. Virtual show()=0;
Pure virtual functions
• Pure virtual functions are used
• if a function doesn't have any use in the base class
• but the function must be implemented by all its derived classes
• Let's take an example,
• Suppose, we have derived Triangle, Square and Circle classes
from the Shape class, and we want to calculate the area of all
these shapes.
• In this case, we can create a pure virtual function
named calculateArea() in the Shape. Since it's a pure virtual
function, all derived classes Triangle, Square and Circle must
include the calculateArea() function with implementation.
Continues…………….
• A pure virtual function doesn't have the
function body and it must end with = 0.
class Shape
{
public:
// creating a pure virtual function
virtual void calculateArea() = 0;
};
Abstract class
• Class with pure virtual function in them are
called Abstract class.

More Related Content

What's hot

Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-classDeepak Singh
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasShahzad Younas
 
Variables in python
Variables in pythonVariables in python
Variables in pythonJaya Kumari
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++Swarup Kumar Boro
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3C# Learning Classes
 
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objectskhaliledapal
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classesasadsardar
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member FunctionsMOHIT AGARWAL
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++Sujan Mia
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7kamal kotecha
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++M Hussnain Ali
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismrattaj
 

What's hot (20)

Oops
OopsOops
Oops
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3
 
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objects
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Java unit2
Java unit2Java unit2
Java unit2
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 

Similar to Object-Oriented Polymorphism: Static vs Dynamic

11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptxAtharvPotdar2
 
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdfManiMala75
 
Presentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptxPresentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptxvishwadeep15
 
Functions_new.pptx
Functions_new.pptxFunctions_new.pptx
Functions_new.pptxYagna15
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3Ahmet Bulut
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxsangeeta borde
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionC++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionHashni T
 
Intro to JavaScript - Week 2: Function
Intro to JavaScript - Week 2: FunctionIntro to JavaScript - Week 2: Function
Intro to JavaScript - Week 2: FunctionJeongbae Oh
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptAmanuelZewdie4
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C ProgrammingAnil Pokhrel
 

Similar to Object-Oriented Polymorphism: Static vs Dynamic (20)

Basics of cpp
Basics of cppBasics of cpp
Basics of cpp
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
 
11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx
 
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
 
Presentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptxPresentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptx
 
Function in C++
Function in C++Function in C++
Function in C++
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
c++ UNIT II.pptx
 
OOP_UnitIII.pdf
OOP_UnitIII.pdfOOP_UnitIII.pdf
OOP_UnitIII.pdf
 
Functions_new.pptx
Functions_new.pptxFunctions_new.pptx
Functions_new.pptx
 
Python functions
Python functionsPython functions
Python functions
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
Unit ii
Unit iiUnit ii
Unit ii
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionC++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversion
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
 
Intro to JavaScript - Week 2: Function
Intro to JavaScript - Week 2: FunctionIntro to JavaScript - Week 2: Function
Intro to JavaScript - Week 2: Function
 
UNIT 3 python.pptx
UNIT 3 python.pptxUNIT 3 python.pptx
UNIT 3 python.pptx
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
Functions
FunctionsFunctions
Functions
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 

Recently uploaded

Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 

Recently uploaded (20)

Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 

Object-Oriented Polymorphism: Static vs Dynamic

  • 2. Definition……. • Polymorphism is a Greek term that means ability to exist in more than one form. For example, an English word “bat” can have two meanings in two different contexts., i.e. a cricket bat and an animal bat. • Similarly, in Object oriented system, polymorphism can be in functions as well as in existing operators( with few exceptions). In broader sense, Polymorphism is of two types: • Static Polymorphism • Dynamic Polymorphism
  • 3. Static Polymorphism • Also known as early binding or compile time polymorphism it’s the ability to bind the function codes with the function calls (provided all the functions have same name) at the compilation time. To implement static polymorphism, we have the concept of function overloading & operator overloading.
  • 4. Dynamic Polymorphism- • Also known as late binding or run time polymorphism, it is the ability to bind the function codes with the function calls (provided all the functions have same name) at the program run time. To implement dynamic polymorphism, we have the concept of virtual functions.
  • 5.
  • 6. Static Polymorphism Function overloading: It’s a phenomenon for a function with same name to exist with different number and types of arguments. Eg. a function called area() can be with two arguments for finding area of a rectangle and the same function area() can be with one argument for finding area of a circle. The syntax for both the functions will be:
  • 7. Syntax: void area( int l, int b) { cout<<”area of rectangle”<< l*b; } void area(int r) { cout <<” area of circle”<< 3.14*r*r; }
  • 8. Explanation…. • When the compiler will come across these two function calls in main(), it will bind the function call to area() with two arguments to first function and function call to area() with one argument to the second function. Hence, this is called static binding of function call with function code that is done at compile time.
  • 9. Operator overloading: • In operator overloading, one operator is capable of existing in more than one forms without changing the actual meaning of the operator. Eg. a “+” operator can be used to add two numbers as well as can be used to concatenate two strings or can add two objects of any class. But in any case it will just add. Any other operation is invalid.
  • 10. Exceptions…. Except for some operators, all the operators can be overloaded. These exceptions are--- . (Member access or dot operator) .*(Pointer to member operator) : :(scope resolution operator) sizeof( ) operator ?: (ternary operator)
  • 11. Syntax for defining operator function The operator can be overloaded through operator function. Its syntax is: return type class-name : : operator op (argument list) { Function body; } Here, the operator function is a member function of any class so it has to be associated with a class when defined outside the class. “operator” is the keyword and “op” is the operator that has to be overloaded.
  • 12. An example….. class xyz { int a, b; public: void getdata( ) { cin>>a>>b; } void operator –( ); void putdata( ) { cout <<a<<b; } }; void xyz : : operator –( ) { a = -a; b = -b; } int main( ) { xyz ob; ob.getdata( ); -ob; // calling the operator function ob.putdata( ); return 0; }
  • 13. • In the previous slide, the example of overloading unary minus operator was shown. • Above operator function can also be used with friend function. It is shown as: friend void operator (xyz &ob); • Here “&” is the reference operator and it refers to the object ob of the class xyz.
  • 14. Pointers to function • Function pointers are widely used in dynamic binding. • The concept of pointer to function acts as a base for pointers to the members(.*). • They are used to select a function dynamically at run time. • General syntax • Datatype (*function_name)();
  • 15. An example • typedef void (*funptr) (int,int); • void add(int i, int j) • { cout<< i<<"+" <<j<<"="<<i+j;} • void subtract( int i, int j) • { • cout<< i<<"-" <<j<<"="<<i-j; • } • int main() { • funptr ptr; • ptr = &add; • ptr(1,2); • cout<<"n"; • ptr =&subtract; • ptr(3,2); • • return 0; • }
  • 16. Pointer to object • Void main() • { • Item x,*ptr; • ptr= &x; • x.getdata();//ptr-> getdata(); • x.show();// ptr->show();//(*ptr).show() • } • The parentheses are necessary because the dot operators has higher precedence than the indirection operator *.
  • 17. This Pointer A pointer to the calling object is referred to as this pointer. It is passed as a default argument to any member function that is called by an object. But, we can also refer to the calling object explicitly in a member function by using this pointer. Eg.
  • 18. An example • class sample • { • int i; • public : • void getdata( ) • { • cin>> this -> i; • } • void putdata( ) • { • cout << this ->i; • • } • };
  • 19. Use of pointer to object • class item { • int code; • float price; • public: • void getdata() • { cout<<"n enter code and price of the item:"; cin>>code>>price;} • void show() • { cout<<"n Code:"<<code; cout<<"nPrice:"<< price;}};int main() { item X, *ptr; ptr= &X; ptr- >getdata(); ptr->show(); return 0;}
  • 20. Pointer to the object of base class. • We always create the pointer to the object of base class. • Pointer to the object of base class are type compatible with pointers to object of a derived class. i.e a single pointer variable can be made to point to object belonging to different class.
  • 21. Virtual function • Virtual functions are those which are preceded by a keyword virtual. • They help in deciding which function has to be called at run time depending upon the type of function pointed by the base pointer.
  • 22. Pure virtual functions • Virtual functions with no definition and which are equated to a zero are called pure virtual functions. They are a basic feature of an Abstract class. • Virtual function defined in a base class is seldom used for performing any task. They servers as a place holders. • Such functions are called do nothing functions or pure virtual functions. • Ex. Virtual show()=0;
  • 23. Pure virtual functions • Pure virtual functions are used • if a function doesn't have any use in the base class • but the function must be implemented by all its derived classes • Let's take an example, • Suppose, we have derived Triangle, Square and Circle classes from the Shape class, and we want to calculate the area of all these shapes. • In this case, we can create a pure virtual function named calculateArea() in the Shape. Since it's a pure virtual function, all derived classes Triangle, Square and Circle must include the calculateArea() function with implementation.
  • 24. Continues……………. • A pure virtual function doesn't have the function body and it must end with = 0. class Shape { public: // creating a pure virtual function virtual void calculateArea() = 0; };
  • 25. Abstract class • Class with pure virtual function in them are called Abstract class.