ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
VIRTUAL FUNCTIONS
AND
ABSTRACT CLASS
(WITH SAMPLE CODE )
VIRTUAL FUNCTION
• A virtual function is a member function which is
declared within a base class and is re-defined
(Overridden) by a derived class.
• When you refer to a derived class object using a
pointer or a reference to the base class, you can call a
virtual function for that object and execute the
derived class’s version of the function.
Virtual functions ensure that the correct function
is called for an object, regardless of the type of
reference (or pointer) used for function call.
They are mainly used to achieve Runtime
polymorphism
Functions are declared with a virtual keyword in
base class.
The resolving of function call is done at Run-
time.
VIRTUAL FUNCTION
 Virtual functions cannot be static and also cannot be a
friend function of another class.
 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 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.
RULES FOR VIRTUAL FUNCTIONS
Virtual function and abstract class
Virtual function and abstract class
NOTE: If we have created a virtual function in the base class and it is
being overridden in the derived class then we don’t need virtual
keyword in the derived class, functions are automatically considered
as virtual functions in the derived class.
Working of virtual functions
(concept of VTABLE and VPTR)
If a class contains a virtual function then compiler itself
does two things:
 If object of that class is created then a virtual
pointer(VPTR) is inserted as a data member of the
class to point to VTABLE of that class. For each new
object created, a new virtual pointer is inserted as a
data member of that class.
 Irrespective of object is created or not, a static array
of function pointer called VTABLE where each cell
contains the address of each virtual function
contained in that class.
Virtual function and abstract class
Virtual function and abstract class
Virtual function and abstract class
Virtual function and abstract class
ABSTRACT CLASSES
Abstract Class is a class which contains at least
one Pure Virtual function in it.
Abstract classes are used to provide an
Interface for its sub classes.
Classes inheriting an Abstract Class must
provide definition to the pure virtual function,
otherwise they will also become abstract class.
CHARACTERISTICS OF ABSTRACT
CLASS
• Abstract class cannot be instantiated, but
pointers and references of Abstract class type can
be created.
• Abstract class can have normal functions and
variables along with a pure virtual function.
• Abstract classes are mainly used for Up casting,
so that its derived classes can use its interface.
• Classes inheriting an Abstract Class must
implement all pure virtual functions, or else they
will become Abstract too.
PURE VIRTUAL FUNCTIONS IN C++
 Pure virtual Functions are virtual functions
with no definition.
 They start with virtual keyword and ends
with = 0. Here is the syntax for a pure
virtual function.
virtual void f() = 0;
Virtual function and abstract class
Why can't we create Object of an Abstract
Class?
When we create a pure virtual function in
Abstract class, we reserve a slot for a function
in the VTABLE, but doesn't put any address in
that slot. Hence the VTABLE will be
incomplete.
As the VTABLE for Abstract class is incomplete,
hence the compiler will not let the creation of
object for such class and will display an error
message whenever you try to do so.
PURE VIRTUAL DEFINITIONS
Pure Virtual functions can be given a small
definition in the Abstract class, which you
want all the derived classes to have. Still you
cannot create object of Abstract class.
Also, the Pure Virtual function must be defined
outside the class definition. If you will define
it inside the class definition, complier will give
an error. Inline pure virtual definition is Illegal.
Virtual function and abstract class
Virtual function and abstract class
THANK YOU

More Related Content

What's hot (20)

Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
PhD Research Scholar
Ìý
User defined functions
User defined functionsUser defined functions
User defined functions
Rokonuzzaman Rony
Ìý
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
Vishesh Jha
Ìý
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
CPD INDIA
Ìý
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
Ìý
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
Ìý
Python-DataAbstarction.pptx
Python-DataAbstarction.pptxPython-DataAbstarction.pptx
Python-DataAbstarction.pptx
Karudaiyar Ganapathy
Ìý
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
Spotle.ai
Ìý
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
Elizabeth alexander
Ìý
Operator overloading
Operator overloadingOperator overloading
Operator overloading
ArunaDevi63
Ìý
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop
Kumar
Ìý
Virtual Functions | Polymorphism | OOP
Virtual Functions | Polymorphism | OOPVirtual Functions | Polymorphism | OOP
Virtual Functions | Polymorphism | OOP
shubham ghimire
Ìý
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
Dabbal Singh Mahara
Ìý
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
Alisha Korpal
Ìý
Inheritance C#
Inheritance C#Inheritance C#
Inheritance C#
Raghuveer Guthikonda
Ìý
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
Ìý
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
Jussi Pohjolainen
Ìý
Lecture 5 sorting and searching
Lecture 5   sorting and searchingLecture 5   sorting and searching
Lecture 5 sorting and searching
Nada G.Youssef
Ìý
Friend function
Friend functionFriend function
Friend function
zindadili
Ìý
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
Darpan Chelani
Ìý
User defined functions
User defined functionsUser defined functions
User defined functions
Rokonuzzaman Rony
Ìý
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
Vishesh Jha
Ìý
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
CPD INDIA
Ìý
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
Ìý
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
Ìý
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
Spotle.ai
Ìý
Operator overloading
Operator overloadingOperator overloading
Operator overloading
ArunaDevi63
Ìý
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop
Kumar
Ìý
Virtual Functions | Polymorphism | OOP
Virtual Functions | Polymorphism | OOPVirtual Functions | Polymorphism | OOP
Virtual Functions | Polymorphism | OOP
shubham ghimire
Ìý
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
Alisha Korpal
Ìý
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
Ìý
Lecture 5 sorting and searching
Lecture 5   sorting and searchingLecture 5   sorting and searching
Lecture 5 sorting and searching
Nada G.Youssef
Ìý
Friend function
Friend functionFriend function
Friend function
zindadili
Ìý
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
Darpan Chelani
Ìý

Similar to Virtual function and abstract class (20)

Polymorphism
PolymorphismPolymorphism
Polymorphism
Prof .Pragati Khade
Ìý
Example for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdfExample for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdf
rajaratna4
Ìý
pure virtual function and abstract classes.ppt
pure virtual function and abstract classes.pptpure virtual function and abstract classes.ppt
pure virtual function and abstract classes.ppt
WaheedAnwar20
Ìý
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
Ìý
22 scheme OOPs with C++ BCS306B_module4.pdf
22 scheme  OOPs with C++ BCS306B_module4.pdf22 scheme  OOPs with C++ BCS306B_module4.pdf
22 scheme OOPs with C++ BCS306B_module4.pdf
sindhus795217
Ìý
Virtual Function
Virtual FunctionVirtual Function
Virtual Function
Lovely Professional University
Ìý
virtual
virtualvirtual
virtual
SangeethaSasi1
Ìý
virtual function
virtual functionvirtual function
virtual function
VENNILAV6
Ìý
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
Mysteriousexpert
Ìý
2 BytesC++ course_2014_c6_ constructors and other tools
2 BytesC++ course_2014_c6_ constructors and other tools2 BytesC++ course_2014_c6_ constructors and other tools
2 BytesC++ course_2014_c6_ constructors and other tools
kinan keshkeh
Ìý
Virtual function complete By Abdul Wahab (moon sheikh)
Virtual function complete By Abdul Wahab (moon sheikh)Virtual function complete By Abdul Wahab (moon sheikh)
Virtual function complete By Abdul Wahab (moon sheikh)
MoonSheikh1
Ìý
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptx
syedabbas594247
Ìý
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics Presentation
Omid Sohrabi
Ìý
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
Ìý
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdf
ismartshanker1
Ìý
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Viyaan Jhiingade
Ìý
Abap Objects for BW
Abap Objects for BWAbap Objects for BW
Abap Objects for BW
Luc Vanrobays
Ìý
6_VirtualFunctions_AbstractClasses.pdf
6_VirtualFunctions_AbstractClasses.pdf6_VirtualFunctions_AbstractClasses.pdf
6_VirtualFunctions_AbstractClasses.pdf
RamiDiab8
Ìý
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objects
khaliledapal
Ìý
Java basics
Java basicsJava basics
Java basics
Shivanshu Purwar
Ìý
Example for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdfExample for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdf
rajaratna4
Ìý
pure virtual function and abstract classes.ppt
pure virtual function and abstract classes.pptpure virtual function and abstract classes.ppt
pure virtual function and abstract classes.ppt
WaheedAnwar20
Ìý
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
Ìý
22 scheme OOPs with C++ BCS306B_module4.pdf
22 scheme  OOPs with C++ BCS306B_module4.pdf22 scheme  OOPs with C++ BCS306B_module4.pdf
22 scheme OOPs with C++ BCS306B_module4.pdf
sindhus795217
Ìý
virtual function
virtual functionvirtual function
virtual function
VENNILAV6
Ìý
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
Mysteriousexpert
Ìý
2 BytesC++ course_2014_c6_ constructors and other tools
2 BytesC++ course_2014_c6_ constructors and other tools2 BytesC++ course_2014_c6_ constructors and other tools
2 BytesC++ course_2014_c6_ constructors and other tools
kinan keshkeh
Ìý
Virtual function complete By Abdul Wahab (moon sheikh)
Virtual function complete By Abdul Wahab (moon sheikh)Virtual function complete By Abdul Wahab (moon sheikh)
Virtual function complete By Abdul Wahab (moon sheikh)
MoonSheikh1
Ìý
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptx
syedabbas594247
Ìý
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics Presentation
Omid Sohrabi
Ìý
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
Ìý
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdf
ismartshanker1
Ìý
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Viyaan Jhiingade
Ìý
Abap Objects for BW
Abap Objects for BWAbap Objects for BW
Abap Objects for BW
Luc Vanrobays
Ìý
6_VirtualFunctions_AbstractClasses.pdf
6_VirtualFunctions_AbstractClasses.pdf6_VirtualFunctions_AbstractClasses.pdf
6_VirtualFunctions_AbstractClasses.pdf
RamiDiab8
Ìý
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objects
khaliledapal
Ìý

More from Shweta Shah (7)

CG_Unit1_SShah.pptx
CG_Unit1_SShah.pptxCG_Unit1_SShah.pptx
CG_Unit1_SShah.pptx
Shweta Shah
Ìý
Windowing clipping
Windowing   clippingWindowing   clipping
Windowing clipping
Shweta Shah
Ìý
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
Shweta Shah
Ìý
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
Shweta Shah
Ìý
Class objects oopm
Class objects oopmClass objects oopm
Class objects oopm
Shweta Shah
Ìý
Introduction to computer graphics and multimedia
Introduction to computer graphics and multimediaIntroduction to computer graphics and multimedia
Introduction to computer graphics and multimedia
Shweta Shah
Ìý
Introduction to Operating Systems
Introduction to Operating SystemsIntroduction to Operating Systems
Introduction to Operating Systems
Shweta Shah
Ìý
CG_Unit1_SShah.pptx
CG_Unit1_SShah.pptxCG_Unit1_SShah.pptx
CG_Unit1_SShah.pptx
Shweta Shah
Ìý
Windowing clipping
Windowing   clippingWindowing   clipping
Windowing clipping
Shweta Shah
Ìý
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
Shweta Shah
Ìý
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
Shweta Shah
Ìý
Class objects oopm
Class objects oopmClass objects oopm
Class objects oopm
Shweta Shah
Ìý
Introduction to computer graphics and multimedia
Introduction to computer graphics and multimediaIntroduction to computer graphics and multimedia
Introduction to computer graphics and multimedia
Shweta Shah
Ìý
Introduction to Operating Systems
Introduction to Operating SystemsIntroduction to Operating Systems
Introduction to Operating Systems
Shweta Shah
Ìý

Recently uploaded (20)

EDL 290F Week 3 - Mountaintop Views (2025).pdf
EDL 290F Week 3  - Mountaintop Views (2025).pdfEDL 290F Week 3  - Mountaintop Views (2025).pdf
EDL 290F Week 3 - Mountaintop Views (2025).pdf
Liz Walsh-Trevino
Ìý
Digital Tools with AI for e-Content Development.pptx
Digital Tools with AI for e-Content Development.pptxDigital Tools with AI for e-Content Development.pptx
Digital Tools with AI for e-Content Development.pptx
Dr. Sarita Anand
Ìý
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
Association for Project Management
Ìý
English 4 Quarter 4 Week 4 Classroom Obs
English 4 Quarter 4 Week 4 Classroom ObsEnglish 4 Quarter 4 Week 4 Classroom Obs
English 4 Quarter 4 Week 4 Classroom Obs
NerissaMendez1
Ìý
Adventure Activities Final By H R Gohil Sir
Adventure Activities Final By H R Gohil SirAdventure Activities Final By H R Gohil Sir
Adventure Activities Final By H R Gohil Sir
GUJARATCOMMERCECOLLE
Ìý
How to Configure Flexible Working Schedule in Odoo 18 Employee
How to Configure Flexible Working Schedule in Odoo 18 EmployeeHow to Configure Flexible Working Schedule in Odoo 18 Employee
How to Configure Flexible Working Schedule in Odoo 18 Employee
Celine George
Ìý
Kaun TALHA quiz Prelims - El Dorado 2025
Kaun TALHA quiz Prelims - El Dorado 2025Kaun TALHA quiz Prelims - El Dorado 2025
Kaun TALHA quiz Prelims - El Dorado 2025
Conquiztadors- the Quiz Society of Sri Venkateswara College
Ìý
Modeling-Simple-Equation-Using-Bar-Models.pptx
Modeling-Simple-Equation-Using-Bar-Models.pptxModeling-Simple-Equation-Using-Bar-Models.pptx
Modeling-Simple-Equation-Using-Bar-Models.pptx
maribethlacno2
Ìý
Fuel part 1.pptx........................
Fuel part 1.pptx........................Fuel part 1.pptx........................
Fuel part 1.pptx........................
ksbhattadcm
Ìý
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptxTRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
PoojaSen20
Ìý
Year 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptxYear 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptx
mansk2
Ìý
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
Ìý
Blind Spots in AI and Formulation Science Knowledge Pyramid (Updated Perspect...
Blind Spots in AI and Formulation Science Knowledge Pyramid (Updated Perspect...Blind Spots in AI and Formulation Science Knowledge Pyramid (Updated Perspect...
Blind Spots in AI and Formulation Science Knowledge Pyramid (Updated Perspect...
Ajaz Hussain
Ìý
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptxTLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
RizaBedayo
Ìý
A PPT on the First Three chapters of Wings of Fire
A PPT on the First Three chapters of Wings of FireA PPT on the First Three chapters of Wings of Fire
A PPT on the First Three chapters of Wings of Fire
Beena E S
Ìý
The Constitution, Government and Law making bodies .
The Constitution, Government and Law making bodies .The Constitution, Government and Law making bodies .
The Constitution, Government and Law making bodies .
saanidhyapatel09
Ìý
N.C. DPI's 2023 Language Diversity Briefing
N.C. DPI's 2023 Language Diversity BriefingN.C. DPI's 2023 Language Diversity Briefing
N.C. DPI's 2023 Language Diversity Briefing
Mebane Rash
Ìý
Research & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptxResearch & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptx
Dr. Sarita Anand
Ìý
QuickBooks Desktop to QuickBooks Online How to Make the Move
QuickBooks Desktop to QuickBooks Online  How to Make the MoveQuickBooks Desktop to QuickBooks Online  How to Make the Move
QuickBooks Desktop to QuickBooks Online How to Make the Move
TechSoup
Ìý
Rass MELAI : an Internet MELA Quiz Prelims - El Dorado 2025
Rass MELAI : an Internet MELA Quiz Prelims - El Dorado 2025Rass MELAI : an Internet MELA Quiz Prelims - El Dorado 2025
Rass MELAI : an Internet MELA Quiz Prelims - El Dorado 2025
Conquiztadors- the Quiz Society of Sri Venkateswara College
Ìý
EDL 290F Week 3 - Mountaintop Views (2025).pdf
EDL 290F Week 3  - Mountaintop Views (2025).pdfEDL 290F Week 3  - Mountaintop Views (2025).pdf
EDL 290F Week 3 - Mountaintop Views (2025).pdf
Liz Walsh-Trevino
Ìý
Digital Tools with AI for e-Content Development.pptx
Digital Tools with AI for e-Content Development.pptxDigital Tools with AI for e-Content Development.pptx
Digital Tools with AI for e-Content Development.pptx
Dr. Sarita Anand
Ìý
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
Association for Project Management
Ìý
English 4 Quarter 4 Week 4 Classroom Obs
English 4 Quarter 4 Week 4 Classroom ObsEnglish 4 Quarter 4 Week 4 Classroom Obs
English 4 Quarter 4 Week 4 Classroom Obs
NerissaMendez1
Ìý
Adventure Activities Final By H R Gohil Sir
Adventure Activities Final By H R Gohil SirAdventure Activities Final By H R Gohil Sir
Adventure Activities Final By H R Gohil Sir
GUJARATCOMMERCECOLLE
Ìý
How to Configure Flexible Working Schedule in Odoo 18 Employee
How to Configure Flexible Working Schedule in Odoo 18 EmployeeHow to Configure Flexible Working Schedule in Odoo 18 Employee
How to Configure Flexible Working Schedule in Odoo 18 Employee
Celine George
Ìý
Modeling-Simple-Equation-Using-Bar-Models.pptx
Modeling-Simple-Equation-Using-Bar-Models.pptxModeling-Simple-Equation-Using-Bar-Models.pptx
Modeling-Simple-Equation-Using-Bar-Models.pptx
maribethlacno2
Ìý
Fuel part 1.pptx........................
Fuel part 1.pptx........................Fuel part 1.pptx........................
Fuel part 1.pptx........................
ksbhattadcm
Ìý
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptxTRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
PoojaSen20
Ìý
Year 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptxYear 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptx
mansk2
Ìý
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
Ìý
Blind Spots in AI and Formulation Science Knowledge Pyramid (Updated Perspect...
Blind Spots in AI and Formulation Science Knowledge Pyramid (Updated Perspect...Blind Spots in AI and Formulation Science Knowledge Pyramid (Updated Perspect...
Blind Spots in AI and Formulation Science Knowledge Pyramid (Updated Perspect...
Ajaz Hussain
Ìý
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptxTLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
RizaBedayo
Ìý
A PPT on the First Three chapters of Wings of Fire
A PPT on the First Three chapters of Wings of FireA PPT on the First Three chapters of Wings of Fire
A PPT on the First Three chapters of Wings of Fire
Beena E S
Ìý
The Constitution, Government and Law making bodies .
The Constitution, Government and Law making bodies .The Constitution, Government and Law making bodies .
The Constitution, Government and Law making bodies .
saanidhyapatel09
Ìý
N.C. DPI's 2023 Language Diversity Briefing
N.C. DPI's 2023 Language Diversity BriefingN.C. DPI's 2023 Language Diversity Briefing
N.C. DPI's 2023 Language Diversity Briefing
Mebane Rash
Ìý
Research & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptxResearch & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptx
Dr. Sarita Anand
Ìý
QuickBooks Desktop to QuickBooks Online How to Make the Move
QuickBooks Desktop to QuickBooks Online  How to Make the MoveQuickBooks Desktop to QuickBooks Online  How to Make the Move
QuickBooks Desktop to QuickBooks Online How to Make the Move
TechSoup
Ìý

Virtual function and abstract class

  • 2. VIRTUAL FUNCTION • A virtual function is a member function which is declared within a base class and is re-defined (Overridden) by a derived class. • When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function.
  • 3. Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call. They are mainly used to achieve Runtime polymorphism Functions are declared with a virtual keyword in base class. The resolving of function call is done at Run- time. VIRTUAL FUNCTION
  • 4.  Virtual functions cannot be static and also cannot be a friend function of another class.  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 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. RULES FOR VIRTUAL FUNCTIONS
  • 7. NOTE: If we have created a virtual function in the base class and it is being overridden in the derived class then we don’t need virtual keyword in the derived class, functions are automatically considered as virtual functions in the derived class.
  • 8. Working of virtual functions (concept of VTABLE and VPTR) If a class contains a virtual function then compiler itself does two things:  If object of that class is created then a virtual pointer(VPTR) is inserted as a data member of the class to point to VTABLE of that class. For each new object created, a new virtual pointer is inserted as a data member of that class.  Irrespective of object is created or not, a static array of function pointer called VTABLE where each cell contains the address of each virtual function contained in that class.
  • 13. ABSTRACT CLASSES Abstract Class is a class which contains at least one Pure Virtual function in it. Abstract classes are used to provide an Interface for its sub classes. Classes inheriting an Abstract Class must provide definition to the pure virtual function, otherwise they will also become abstract class.
  • 14. CHARACTERISTICS OF ABSTRACT CLASS • Abstract class cannot be instantiated, but pointers and references of Abstract class type can be created. • Abstract class can have normal functions and variables along with a pure virtual function. • Abstract classes are mainly used for Up casting, so that its derived classes can use its interface. • Classes inheriting an Abstract Class must implement all pure virtual functions, or else they will become Abstract too.
  • 15. PURE VIRTUAL FUNCTIONS IN C++  Pure virtual Functions are virtual functions with no definition.  They start with virtual keyword and ends with = 0. Here is the syntax for a pure virtual function. virtual void f() = 0;
  • 17. Why can't we create Object of an Abstract Class? When we create a pure virtual function in Abstract class, we reserve a slot for a function in the VTABLE, but doesn't put any address in that slot. Hence the VTABLE will be incomplete. As the VTABLE for Abstract class is incomplete, hence the compiler will not let the creation of object for such class and will display an error message whenever you try to do so.
  • 18. PURE VIRTUAL DEFINITIONS Pure Virtual functions can be given a small definition in the Abstract class, which you want all the derived classes to have. Still you cannot create object of Abstract class. Also, the Pure Virtual function must be defined outside the class definition. If you will define it inside the class definition, complier will give an error. Inline pure virtual definition is Illegal.