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 Function
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
• You can redefine or overload most of the built-in
operators available in C++. Thus, a programmer can
use operators with user-defined types as well.
• Overloaded operators are functions with special
names: the keyword "operator" followed by the
symbol for the operator being defined. Like any
other function, an overloaded operator has a return
type and a parameter list.
• Box operator+(const Box&);
• declares the addition operator that can be used
to add two Box objects and returns final Box object.
Most overloaded operators may be defined as
ordinary non-member functions or as class member
functions. In case we define above function as non-
member function of a class then we would have to
pass two arguments for each operand as follows −
• Box operator+(const Box&, const Box&);
Operator Overloading
Virtual Function
Virtual Function
Operator Overloading
Virtual Function
Virtual Function
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 Function
• 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
• They Must be declared in public section of class.and must be member of
some class.
• Virtual functions cannot be static.
• Virtual functions should be accessed using pointer or reference of base
class type to achieve run time polymorphism
• .
• The prototype of virtual functions should be same in base as well as in
derived class.
• They are always defined in base class and overridden in derived class. It is
not mandatory for derived class to override (or re-define the virtual
function), in that case base class version of function is used.
• A class may have virtual destructor but it cannot have a virtual constructor.
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
derived class the option of overriding the base class
virtual function
• a pure virtual function doesn't 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 atleast 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.

More Related Content

PPT
Constructors and destructors in C++ part 2
PPSX
Object oriented concepts & programming (2620003)
PDF
Constructors destructors
PPT
C++ classes tutorials
PPT
Oop Constructor Destructors Constructor Overloading lecture 2
PPTX
Constructor in java
PDF
Inheritance
PPTX
Constructors & Destructors
Constructors and destructors in C++ part 2
Object oriented concepts & programming (2620003)
Constructors destructors
C++ classes tutorials
Oop Constructor Destructors Constructor Overloading lecture 2
Constructor in java
Inheritance
Constructors & Destructors

What's hot (20)

PDF
2 BytesC++ course_2014_c6_ constructors and other tools
PPTX
2CPP04 - Objects and Classes
PPT
106da session5 c++
PPTX
polymorphism and virtual function
PPTX
Object oriented java script
PPT
16 virtual function
PPTX
PPT
Of Lambdas and LINQ
PDF
Constructors and destructors
PPTX
Virtual function complete By Abdul Wahab (moon sheikh)
PPTX
Polymorphism
PPTX
Presentation 1st
PPSX
Writing code that writes code - Nguyen Luong
PPT
JavaScript - Programming Languages course
PPTX
Polymorphism 140527082302-phpapp01
PPSX
Polymorphism
PPT
Object-Oriented Programming Using C++
PPT
Static and Dynamic polymorphism in C++
PDF
A COMPLETE FILE FOR C++
2 BytesC++ course_2014_c6_ constructors and other tools
2CPP04 - Objects and Classes
106da session5 c++
polymorphism and virtual function
Object oriented java script
16 virtual function
Of Lambdas and LINQ
Constructors and destructors
Virtual function complete By Abdul Wahab (moon sheikh)
Polymorphism
Presentation 1st
Writing code that writes code - Nguyen Luong
JavaScript - Programming Languages course
Polymorphism 140527082302-phpapp01
Polymorphism
Object-Oriented Programming Using C++
Static and Dynamic polymorphism in C++
A COMPLETE FILE FOR C++
Ad

Similar to Virtual Function (20)

PPT
VIRTUAL FUNCTIONS c++ -POLYMORPHISM.ppt
PDF
22 scheme OOPs with C++ BCS306B_module4.pdf
PPTX
B.sc CSIT 2nd semester C++ Unit6
PPTX
Virtual function and abstract class
PPTX
Dynamic Polymorphism in C++
PPTX
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
PDF
polymorphism for b.tech iii year students
PDF
Example for Virtual and Pure Virtual function.pdf
PPT
Polymorphism in C++ for beginners reference
PPTX
Polymorphism
PPTX
Virtual, Pure Virtual functions and Abstract class.pptx
PPTX
Virtual function
PPTX
OOPS & C++(UNIT 4)
PPTX
PPT
Virtual Function and Polymorphism.ppt
PPTX
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
PPTX
6. Virtual base class.pptx and virtual function
PDF
polymorpisum-140106223024-phpapp01.pdf
PPTX
polymorphism ppt
PDF
Classes-and-Objects-in-C++.pdf
VIRTUAL FUNCTIONS c++ -POLYMORPHISM.ppt
22 scheme OOPs with C++ BCS306B_module4.pdf
B.sc CSIT 2nd semester C++ Unit6
Virtual function and abstract class
Dynamic Polymorphism in C++
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
polymorphism for b.tech iii year students
Example for Virtual and Pure Virtual function.pdf
Polymorphism in C++ for beginners reference
Polymorphism
Virtual, Pure Virtual functions and Abstract class.pptx
Virtual function
OOPS & C++(UNIT 4)
Virtual Function and Polymorphism.ppt
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
6. Virtual base class.pptx and virtual function
polymorpisum-140106223024-phpapp01.pdf
polymorphism ppt
Classes-and-Objects-in-C++.pdf
Ad

More from Carelon Global Solutions (20)

PPT
Constructor and destructor in C++
PPT
Classes and objects
PPT
DOC
Major project synopsis format
DOCX
Docslide.net soyabean milk-project-class-12
DOCX
Physics first page
DOCX
Uses of transformer
DOCX
Theory and working
DOCX
Main page vishnu
DOCX
Main page v physics
DOCX
Main page saurabh
DOCX
DOCX
Efficiency and energy losses
DOCX
DOCX
DOCX
Acknowledgement
DOCX
Principle construction
DOCX
Theory and procedure
Constructor and destructor in C++
Classes and objects
Major project synopsis format
Docslide.net soyabean milk-project-class-12
Physics first page
Uses of transformer
Theory and working
Main page vishnu
Main page v physics
Main page saurabh
Efficiency and energy losses
Acknowledgement
Principle construction
Theory and procedure

Recently uploaded (20)

PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
master seminar digital applications in india
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Pharma ospi slides which help in ospi learning
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
01-Introduction-to-Information-Management.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Microbial disease of the cardiovascular and lymphatic systems
A systematic review of self-coping strategies used by university students to ...
Microbial diseases, their pathogenesis and prophylaxis
master seminar digital applications in india
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Pharma ospi slides which help in ospi learning
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
RMMM.pdf make it easy to upload and study
2.FourierTransform-ShortQuestionswithAnswers.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Final Presentation General Medicine 03-08-2024.pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
O7-L3 Supply Chain Operations - ICLT Program
01-Introduction-to-Information-Management.pdf

Virtual Function

  • 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. • You can redefine or overload most of the built-in operators available in C++. Thus, a programmer can use operators with user-defined types as well. • Overloaded operators are functions with special names: the keyword "operator" followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list. • Box operator+(const Box&);
  • 8. • declares the addition operator that can be used to add two Box objects and returns final Box object. Most overloaded operators may be defined as ordinary non-member functions or as class member functions. In case we define above function as non- member function of a class then we would have to pass two arguments for each operand as follows − • Box operator+(const Box&, const Box&);
  • 15. 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.
  • 16. • 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
  • 17. • One should note that if polymorphism if not explicilty specified is run time polymorphism. • Run time polymorphism is implemented using virtual functions.
  • 18. 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.
  • 20. • 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.
  • 21. 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
  • 22. • 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.
  • 23. • 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
  • 24. • They Must be declared in public section of class.and must be member of some class. • Virtual functions cannot be static. • Virtual functions should be accessed using pointer or reference of base class type to achieve run time polymorphism • . • The prototype of virtual functions should be same in base as well as in derived class. • They are always defined in base class and overridden in derived class. It is not mandatory for derived class to override (or re-define the virtual function), in that case base class version of function is used. • A class may have virtual destructor but it cannot have a virtual constructor.
  • 25. 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;
  • 26. • 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
  • 27. • The main difference between pure virtual function and virtual function is that • a virtual function has a body and provide the derived class the option of overriding the base class virtual function • a pure virtual function doesn't have any body and derive class must override the base class pure virtual function.
  • 28. • 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 atleast one pure virtual function is known as abstract class
  • 29. • 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.
  • 30. 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 .
  • 31. • 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
  • 32. • 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
  • 33. 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.