SlideShare a Scribd company logo
VIRTUAL FUNCTIONS
POLYMORPHISM
‱ Polymorphism means many (poly) shapes (morph)
‱ It can be defined one interface multiple methods
which means that one interface can be used to
perform different but related activities.
‱ It is achieved by two ways:
‱ Overloading.
‱ Overriding.
Classification of Polymorphism
‱ It can be classified in two ways on the basis of
binding performed by the compiler for all the
related but different operations having a common
name.
‱ The concept of binding refers to the linking of
function call to the code of the function to be
executed in response to the function call.
(i)Compile time(or static) polymorphism.
(ii)Run time (Or Dynamic) polymorphism.
VIRTUAL FUNCTIONS  c++ -POLYMORPHISM.ppt
Compile Time Polymorphism
‱ In compile time polymorphism ,static binding is
performed.
‱ In this compiler makes the decision regarding
selection of appropriate function to be called in
response to function call at compile time.
‱ This is because all the address information requires
to call a function is known at compile time.
‱ It is also known as early binding as decision of
binding is made by the compiler at the earliest
possible moment.
‱ The advantage is its efficiency as it often requires
less memory and function calls are faster.
‱ The disadvantage is lack of flexiability.
‱ The compile time polymorphism is implemented
using function overloding and operator overloading
Run time(Dynamic)Polymorphism
‱ In this dynamic binding is performed
‱ In dynamic binding the decision regarding the
selection of appropriate function to be called is
made by compiler at run time.
‱ This is because the information pertaining to the
selection of appropriate function definition
corresponding to a function call is known only at
the run time.
‱ It is also called the late binding as the compiler
delays the binding decision until run time.
‱ The advantage is that it allows greater flexiability by
enabling user to create class libraries that can be
reused and extended as per requirement
‱ The disadvantage is that there is little less od
execution speed as compiler will have to perform
certain overheads at run time
‱ One should note that if polymorphism if not
explicilty specified is run time polymorphism.
‱ Run time polymorphism is implemented using
virtual functions.
Pointer to derieved class object
‱ Base class pointers can point to derived class objects and
invoke members functions that manipulate those objects.
‱ The fact that the pointer of the base class can point to objects
if the derived classes is absolutely fine because each derived
class object is an object of its base class also.
‱ Even though the base class pointer has been assigned the
address of the object of one of its derived classes it still cannot
access the public members which are defined in the derived
class.
VIRTUAL FUNCTIONS  c++ -POLYMORPHISM.ppt
‱ It can access only those members that are
in-heritated from the base class to derived
class
‱ Any reference to the same named inherited the
members using base class pointer will result in
accessing the base class member and not the
member of derived class.
VIRTUAL FUNCTIONS
‱ A virtual function is a member function declared in
the base class using the keyword virtual whose
functionality is redefined(same function name) by
its derieved classes.
‱ The virtual function declared in the base class
represent the single interface and its redefinition by
the derieved classes implements operations specific
of each derieved class
‱ To implement run time polymorphism using virtual
function ,it must be invoked through the base class
pointer that can contain the address of objects of
different derieved classes.
‱ When the virtual function is invoked through the
base class pointer the compiler chooses the
appropriate member function of the derieved class
at run time depending upon the contents of base
class pointer and not the type of pointer.
‱ Thus by making the base class pointer pointing to
objects of different classes ,the different versions of
virtual functions can be called at run time
PURE VIRTUAL FUNCTIONS
‱ A virtual function is defined inside the base class
and redefined in the derived classes.
‱ But in most situations,a virtual function defined in
the base class does not have any meaningful
operation for it to perform.
‱ A better way to change the virtual function
cal_area() in the base class is:
virtual void cal_area() =0;
‱ The above statement is not an assignment
statement but it’s a way to inform the compiler
that the function has no body.
‱ Such a virtual function having intializer=0 in its
declaration and whichdoes not provide any
implementation (no body) is known as pure virtual
functions.
‱ A pure virtual function is also known as dummy
functions or do nothing function. they serve merely
as an interface to be overriden by subsequent
derieved classes
‱ The main difference between pure virtual function
and virtual function is that
‱ :-a virtual function has a body and provide the
derieved class the option of overriding the base
class virtual function
‱ a pure virtual function doesnot have any body and
derive class must override the base class pure
virtual function.
‱ Pure virtual function doesnot have any body so a
class in which a pure virtual function is declared
cannot be instantiated,i.e we cannot create objects
of such a class.This is known as ABSTRACT CLASS.
‱ A class whose objects cannot be created and
contain aatleast one pure virtual function is known
as abstract class
‱ We cannot create the objects of a abstract class
and if we try it gives a error message.
‱ But we can use abstract base class to declare
pointers for storing the address of the objects of
concerete classes derieved from it.
Virtual Destructor
‱ Destructor can also be declared as virtual
‱ If we want to create an object of a derived class
dynamically using the base class pointer. If we
destroy this derived object with non-virtual
destructor explicitly using the delete operator in
association with the base class pointer then only
the destructor of the base class is executed and not
the derived class’s destructor .
‱ The reason behind the non execution of derieved
class destructor is that the compiler uses static
binding when calling the destructor.
‱ The solution to this problem is to make the
destructor to be dynamically bound which is
achieved by making the base class destructor as
virtual
‱ Now if you destroy the derieved object explicity
using delete operator in association with the base
class pointer,the destructor of appropriate derieved
class is called depending upon the object to which
pointer of base class points
this pointer
‱ Every object has a special pointer "this" which
points to the object itself.
‱ This pointer is accessible to all members of the
class but not to any static members of the class.
‱ Can be used to find the address of the object in
which the function is a member.
‱ Presence of this pointer is not included in the sizeof
calculations.
Object Slicing
‱ Object slicing is a concept where additional
attributes of a derived class object is sliced to form
a base class object.
‱ Object slicing doesn't occur when pointers or
references to objects are passed as function
arguments since both the pointers are of the same
size.
‱ Object slicing will be noticed when pass by value is
done for a derived class object for a function
accepting base class object.
‱ Object slicing could be prevented by making the
base class function pure virtual there by disallowing
object creation.
‱ When a Derived Class object is assigned to Base class,
the base class' contents in the derived object are
copied to the base class leaving behind the derived
class specific contents. This is referred as Object
Slicing. That is, the base class object can access only
the base class members. This also implies the
separation of base class members from derived class
members has happened.
‱
class base
{
public:
int i, j;
};
class derived : public base
{
public:
int k;
};
int main()
{
base b;
derived d;
b=d;
return 0;
}
‱ here b contains i and j where as d contains i, j& k. On
assignment only i and j of the d get copied into i and j
of b. k does not get copied. on the effect object d got
sliced.

More Related Content

Similar to VIRTUAL FUNCTIONS c++ -POLYMORPHISM.ppt (20)

PPTX
Quick Interview Preparation for C# All Concepts
Karmanjay Verma
 
PPTX
C++ training
PL Sharma
 
PPTX
c++.pptxwjwjsijsnsksomammaoansnksooskskk
mitivete
 
PPTX
object oriented programming language.pptx
syedabbas594247
 
PPTX
Polymorphism
Prof .Pragati Khade
 
PPTX
Design patterns
Alok Guha
 
PPTX
Object Oriented Programming Tutorial.pptx
ethiouniverse
 
PDF
Javascript classes and scoping
Patrick Sheridan
 
PDF
2 BytesC++ course_2014_c6_ constructors and other tools
kinan keshkeh
 
PPTX
Abstraction in java.pptx
AsifMulani17
 
PDF
Javascript for Intermediates
Ankit Agrawal
 
PPTX
Object oriented programming
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Class Members Access/Visibility Guide (Checklist)
Jayasree Perilakkalam
 
PPTX
Virtual function and abstract class
Shweta Shah
 
PPTX
Complete PPT about the Java lokesh kept it
lokeshpappaka10
 
PDF
Introduction to C++ Class & Objects. Book Notes
DSMS Group of Institutes
 
PPTX
Object oriented java script
vivek p s
 
PPTX
11.C++Polymorphism [Autosaved].pptx
AtharvPotdar2
 
PPTX
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
PDF
Working With Concurrency In Java 8
Heartin Jacob
 
Quick Interview Preparation for C# All Concepts
Karmanjay Verma
 
C++ training
PL Sharma
 
c++.pptxwjwjsijsnsksomammaoansnksooskskk
mitivete
 
object oriented programming language.pptx
syedabbas594247
 
Polymorphism
Prof .Pragati Khade
 
Design patterns
Alok Guha
 
Object Oriented Programming Tutorial.pptx
ethiouniverse
 
Javascript classes and scoping
Patrick Sheridan
 
2 BytesC++ course_2014_c6_ constructors and other tools
kinan keshkeh
 
Abstraction in java.pptx
AsifMulani17
 
Javascript for Intermediates
Ankit Agrawal
 
Class Members Access/Visibility Guide (Checklist)
Jayasree Perilakkalam
 
Virtual function and abstract class
Shweta Shah
 
Complete PPT about the Java lokesh kept it
lokeshpappaka10
 
Introduction to C++ Class & Objects. Book Notes
DSMS Group of Institutes
 
Object oriented java script
vivek p s
 
11.C++Polymorphism [Autosaved].pptx
AtharvPotdar2
 
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
Working With Concurrency In Java 8
Heartin Jacob
 

Recently uploaded (20)

PPTX
Presentation on Foundation Design for Civil Engineers.pptx
KamalKhan563106
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
PDF
MOBILE AND WEB BASED REMOTE BUSINESS MONITORING SYSTEM
ijait
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PPTX
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
PPT
Oxygen Co2 Transport in the Lungs(Exchange og gases)
SUNDERLINSHIBUD
 
PDF
UNIT-4-FEEDBACK AMPLIFIERS AND OSCILLATORS (1).pdf
Sridhar191373
 
PPTX
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
PDF
BioSensors glucose monitoring, cholestrol
nabeehasahar1
 
PPTX
NEUROMOROPHIC nu iajwojeieheueueueu.pptx
knkoodalingam39
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPTX
Electron Beam Machining for Production Process
Rajshahi University of Engineering & Technology(RUET), Bangladesh
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PPTX
EC3551-Transmission lines Demo class .pptx
Mahalakshmiprasannag
 
PPTX
REINFORCEMENT AS CONSTRUCTION MATERIALS.pptx
mohaiminulhaquesami
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PDF
Additional Information in midterm CPE024 (1).pdf
abolisojoy
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PDF
IoT - Unit 2 (Internet of Things-Concepts) - PPT.pdf
dipakraut82
 
Presentation on Foundation Design for Civil Engineers.pptx
KamalKhan563106
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
MOBILE AND WEB BASED REMOTE BUSINESS MONITORING SYSTEM
ijait
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
Oxygen Co2 Transport in the Lungs(Exchange og gases)
SUNDERLINSHIBUD
 
UNIT-4-FEEDBACK AMPLIFIERS AND OSCILLATORS (1).pdf
Sridhar191373
 
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
BioSensors glucose monitoring, cholestrol
nabeehasahar1
 
NEUROMOROPHIC nu iajwojeieheueueueu.pptx
knkoodalingam39
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
Electron Beam Machining for Production Process
Rajshahi University of Engineering & Technology(RUET), Bangladesh
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
EC3551-Transmission lines Demo class .pptx
Mahalakshmiprasannag
 
REINFORCEMENT AS CONSTRUCTION MATERIALS.pptx
mohaiminulhaquesami
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Additional Information in midterm CPE024 (1).pdf
abolisojoy
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
IoT - Unit 2 (Internet of Things-Concepts) - PPT.pdf
dipakraut82
 
Ad

VIRTUAL FUNCTIONS c++ -POLYMORPHISM.ppt

  • 2. POLYMORPHISM ‱ Polymorphism means many (poly) shapes (morph) ‱ It can be defined one interface multiple methods which means that one interface can be used to perform different but related activities. ‱ It is achieved by two ways: ‱ Overloading. ‱ Overriding.
  • 3. Classification of Polymorphism ‱ It can be classified in two ways on the basis of binding performed by the compiler for all the related but different operations having a common name. ‱ The concept of binding refers to the linking of function call to the code of the function to be executed in response to the function call. (i)Compile time(or static) polymorphism. (ii)Run time (Or Dynamic) polymorphism.
  • 5. Compile Time Polymorphism ‱ In compile time polymorphism ,static binding is performed. ‱ In this compiler makes the decision regarding selection of appropriate function to be called in response to function call at compile time. ‱ This is because all the address information requires to call a function is known at compile time.
  • 6. ‱ It is also known as early binding as decision of binding is made by the compiler at the earliest possible moment. ‱ The advantage is its efficiency as it often requires less memory and function calls are faster. ‱ The disadvantage is lack of flexiability. ‱ The compile time polymorphism is implemented using function overloding and operator overloading
  • 7. Run time(Dynamic)Polymorphism ‱ In this dynamic binding is performed ‱ In dynamic binding the decision regarding the selection of appropriate function to be called is made by compiler at run time. ‱ This is because the information pertaining to the selection of appropriate function definition corresponding to a function call is known only at the run time.
  • 8. ‱ It is also called the late binding as the compiler delays the binding decision until run time. ‱ The advantage is that it allows greater flexiability by enabling user to create class libraries that can be reused and extended as per requirement ‱ The disadvantage is that there is little less od execution speed as compiler will have to perform certain overheads at run time
  • 9. ‱ One should note that if polymorphism if not explicilty specified is run time polymorphism. ‱ Run time polymorphism is implemented using virtual functions.
  • 10. Pointer to derieved class object ‱ Base class pointers can point to derived class objects and invoke members functions that manipulate those objects. ‱ The fact that the pointer of the base class can point to objects if the derived classes is absolutely fine because each derived class object is an object of its base class also. ‱ Even though the base class pointer has been assigned the address of the object of one of its derived classes it still cannot access the public members which are defined in the derived class.
  • 12. ‱ It can access only those members that are in-heritated from the base class to derived class ‱ Any reference to the same named inherited the members using base class pointer will result in accessing the base class member and not the member of derived class.
  • 13. VIRTUAL FUNCTIONS ‱ A virtual function is a member function declared in the base class using the keyword virtual whose functionality is redefined(same function name) by its derieved classes. ‱ The virtual function declared in the base class represent the single interface and its redefinition by the derieved classes implements operations specific of each derieved class
  • 14. ‱ To implement run time polymorphism using virtual function ,it must be invoked through the base class pointer that can contain the address of objects of different derieved classes. ‱ When the virtual function is invoked through the base class pointer the compiler chooses the appropriate member function of the derieved class at run time depending upon the contents of base class pointer and not the type of pointer.
  • 15. ‱ Thus by making the base class pointer pointing to objects of different classes ,the different versions of virtual functions can be called at run time
  • 16. PURE VIRTUAL FUNCTIONS ‱ A virtual function is defined inside the base class and redefined in the derived classes. ‱ But in most situations,a virtual function defined in the base class does not have any meaningful operation for it to perform. ‱ A better way to change the virtual function cal_area() in the base class is: virtual void cal_area() =0;
  • 17. ‱ The above statement is not an assignment statement but it’s a way to inform the compiler that the function has no body. ‱ Such a virtual function having intializer=0 in its declaration and whichdoes not provide any implementation (no body) is known as pure virtual functions. ‱ A pure virtual function is also known as dummy functions or do nothing function. they serve merely as an interface to be overriden by subsequent derieved classes
  • 18. ‱ The main difference between pure virtual function and virtual function is that ‱ :-a virtual function has a body and provide the derieved class the option of overriding the base class virtual function ‱ a pure virtual function doesnot have any body and derive class must override the base class pure virtual function.
  • 19. ‱ Pure virtual function doesnot have any body so a class in which a pure virtual function is declared cannot be instantiated,i.e we cannot create objects of such a class.This is known as ABSTRACT CLASS. ‱ A class whose objects cannot be created and contain aatleast one pure virtual function is known as abstract class
  • 20. ‱ We cannot create the objects of a abstract class and if we try it gives a error message. ‱ But we can use abstract base class to declare pointers for storing the address of the objects of concerete classes derieved from it.
  • 21. Virtual Destructor ‱ Destructor can also be declared as virtual ‱ If we want to create an object of a derived class dynamically using the base class pointer. If we destroy this derived object with non-virtual destructor explicitly using the delete operator in association with the base class pointer then only the destructor of the base class is executed and not the derived class’s destructor .
  • 22. ‱ The reason behind the non execution of derieved class destructor is that the compiler uses static binding when calling the destructor. ‱ The solution to this problem is to make the destructor to be dynamically bound which is achieved by making the base class destructor as virtual
  • 23. ‱ Now if you destroy the derieved object explicity using delete operator in association with the base class pointer,the destructor of appropriate derieved class is called depending upon the object to which pointer of base class points
  • 24. this pointer ‱ Every object has a special pointer "this" which points to the object itself. ‱ This pointer is accessible to all members of the class but not to any static members of the class. ‱ Can be used to find the address of the object in which the function is a member. ‱ Presence of this pointer is not included in the sizeof calculations.
  • 25. Object Slicing ‱ Object slicing is a concept where additional attributes of a derived class object is sliced to form a base class object. ‱ Object slicing doesn't occur when pointers or references to objects are passed as function arguments since both the pointers are of the same size.
  • 26. ‱ Object slicing will be noticed when pass by value is done for a derived class object for a function accepting base class object. ‱ Object slicing could be prevented by making the base class function pure virtual there by disallowing object creation.
  • 27. ‱ When a Derived Class object is assigned to Base class, the base class' contents in the derived object are copied to the base class leaving behind the derived class specific contents. This is referred as Object Slicing. That is, the base class object can access only the base class members. This also implies the separation of base class members from derived class members has happened. ‱
  • 28. class base { public: int i, j; }; class derived : public base { public: int k; }; int main() { base b; derived d; b=d; return 0; }
  • 29. ‱ here b contains i and j where as d contains i, j& k. On assignment only i and j of the d get copied into i and j of b. k does not get copied. on the effect object d got sliced.