SlideShare a Scribd company logo
The Islamia University
of Bahawalpur
Department Of Computer
Science
Presented By:
WAHEED ANWER
Virtual Functions
– Define a virtual function in the base class. The
word virtual appears only in the base class
– Virtual function in base class stays virtual in all
the derived classes
– It can be overridden in the derived classes
– But, a derived class is not required to re-
implement a virtual function. If it does not, the
base class version is used
Static Binding
• Static binding is the compile-time
determination of which function to
call for a particular object based
on the type of the formal
parameter
• When pass-by-value is used, static binding occurs
Dynamic Binding
• Is the run-time determination of which
function to call for a particular object of a
derived class based on the type of the
argument
• Declaring a member function to be virtual
instructs the compiler to generate code
that guarantees dynamic binding
• Dynamic binding requires pass-by-
reference
Pure Virtual Function
• A pure virtual function is a virtual member
function of a base class that must be
overridden.
• A pure virtual function is a virtual member
function declared in a manner similar to
the following:
virtual void showInfo(void) = 0;
Abstract Class
• A class becomes an abstract base class
when it contains one or more pure virtual
functions.
• An abstract base class is not instantiated,
but other classes are derived from it.
Abstract Classes & Pure Virtual Functions
class Shape //Abstract
{
public :
//Pure virtual Function
virtual void draw() = 0;
}
• A class with one or more pure
virtual functions is an Abstract
Class
• Objects of abstract class can’t be
created
Shape s; // error : variable of an abstract class
• Some classes exist logically but not physically.
• Example : Shape
– Shape s;
– Shape makes sense only as a base of some classes
derived from it. Serves as a “category”
– Hence instantiation of such a class must be prevented
Example
10
Shape
virtual void draw()
Circle
public void draw()
Triangle
public void draw()
11
• A pure virtual function not defined in the
derived class remains a pure virtual function.
• Hence derived class also becomes abstract
class Circle : public Shape { //No draw() - Abstract
public :
void print(){
cout << “I am a circle” << endl;
}
class Rectangle : public Shape {
public :
void draw(){ // Override Shape::draw()
cout << “Drawing Rectangle” << endl;
}
Rectangle r; // Valid
Circle c; // error : variable of an abstract class
Pure Virtual Functions: Summary
• Pure virtual functions are useful because they
make explicit the abstractness of a class
• Tell both the user and the compiler how it was
intended to be used
• Note : It is a good idea to keep the common code
as close as possible to the root of you hierarchy
12
Virtual base class
13
pure virtual function and abstract classes.ppt

More Related Content

Similar to pure virtual function and abstract classes.ppt

Lecture6.ppt
Lecture6.pptLecture6.ppt
Lecture6.ppt
ammu241754
 
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
 
6_VirtualFunctions_AbstractClasses.pdf
6_VirtualFunctions_AbstractClasses.pdf6_VirtualFunctions_AbstractClasses.pdf
6_VirtualFunctions_AbstractClasses.pdf
RamiDiab8
 
virtual function
virtual functionvirtual function
virtual function
VENNILAV6
 
04cpp inh
04cpp inh04cpp inh
04cpp inh
Jihin Raju
 
Abstrcation
AbstrcationAbstrcation
Abstrcation
abdullah619
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Prof .Pragati Khade
 
ABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.pptABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
Rabin BK
 
OOPC_Unit-I.pdf
OOPC_Unit-I.pdfOOPC_Unit-I.pdf
OOPC_Unit-I.pdf
KRaviChythanyaCSE
 
2 BytesC++ course_2014_c12_ polymorphism
2 BytesC++ course_2014_c12_ polymorphism2 BytesC++ course_2014_c12_ polymorphism
2 BytesC++ course_2014_c12_ polymorphism
kinan keshkeh
 
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
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
rayanbabur
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Maaz Hasan
 
Constructor
ConstructorConstructor
Constructor
abhay singh
 
Inheritance, Polymorphism, and Virtual Functions.pptx
Inheritance, Polymorphism, and Virtual Functions.pptxInheritance, Polymorphism, and Virtual Functions.pptx
Inheritance, Polymorphism, and Virtual Functions.pptx
MrNikhilMohanShinde
 
8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#
Sireesh K
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
PRIYACHAURASIYA25
 
Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)
Hassan Hashmi
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 

Similar to pure virtual function and abstract classes.ppt (20)

Lecture6.ppt
Lecture6.pptLecture6.ppt
Lecture6.ppt
 
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)
 
6_VirtualFunctions_AbstractClasses.pdf
6_VirtualFunctions_AbstractClasses.pdf6_VirtualFunctions_AbstractClasses.pdf
6_VirtualFunctions_AbstractClasses.pdf
 
virtual function
virtual functionvirtual function
virtual function
 
04cpp inh
04cpp inh04cpp inh
04cpp inh
 
Abstrcation
AbstrcationAbstrcation
Abstrcation
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
ABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.pptABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.ppt
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
OOPC_Unit-I.pdf
OOPC_Unit-I.pdfOOPC_Unit-I.pdf
OOPC_Unit-I.pdf
 
2 BytesC++ course_2014_c12_ polymorphism
2 BytesC++ course_2014_c12_ polymorphism2 BytesC++ course_2014_c12_ polymorphism
2 BytesC++ course_2014_c12_ polymorphism
 
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
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Constructor
ConstructorConstructor
Constructor
 
Inheritance, Polymorphism, and Virtual Functions.pptx
Inheritance, Polymorphism, and Virtual Functions.pptxInheritance, Polymorphism, and Virtual Functions.pptx
Inheritance, Polymorphism, and Virtual Functions.pptx
 
8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
 

Recently uploaded

4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
Social Samosa
 
原版一比一弗林德斯大学毕业证(Flinders毕业证书)如何办理
原版一比一弗林德斯大学毕业证(Flinders毕业证书)如何办理原版一比一弗林德斯大学毕业证(Flinders毕业证书)如何办理
原版一比一弗林德斯大学毕业证(Flinders毕业证书)如何办理
a9qfiubqu
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
apvysm8
 
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
mkkikqvo
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
bopyb
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
aqzctr7x
 
writing report business partner b1+ .pdf
writing report business partner b1+ .pdfwriting report business partner b1+ .pdf
writing report business partner b1+ .pdf
VyNguyen709676
 
一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
slg6lamcq
 
Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...
Bill641377
 
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Aggregage
 
End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024
Lars Albertsson
 
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Kaxil Naik
 
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
nuttdpt
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
ihavuls
 
一比一原版(CU毕业证)卡尔顿大学毕业证如何办理
一比一原版(CU毕业证)卡尔顿大学毕业证如何办理一比一原版(CU毕业证)卡尔顿大学毕业证如何办理
一比一原版(CU毕业证)卡尔顿大学毕业证如何办理
bmucuha
 
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
z6osjkqvd
 
UofT毕业证如何办理
UofT毕业证如何办理UofT毕业证如何办理
UofT毕业证如何办理
exukyp
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
Timothy Spann
 
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
Timothy Spann
 
Monthly Management report for the Month of May 2024
Monthly Management report for the Month of May 2024Monthly Management report for the Month of May 2024
Monthly Management report for the Month of May 2024
facilitymanager11
 

Recently uploaded (20)

4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
 
原版一比一弗林德斯大学毕业证(Flinders毕业证书)如何办理
原版一比一弗林德斯大学毕业证(Flinders毕业证书)如何办理原版一比一弗林德斯大学毕业证(Flinders毕业证书)如何办理
原版一比一弗林德斯大学毕业证(Flinders毕业证书)如何办理
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
 
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
 
writing report business partner b1+ .pdf
writing report business partner b1+ .pdfwriting report business partner b1+ .pdf
writing report business partner b1+ .pdf
 
一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
 
Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...
 
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
 
End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024
 
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
 
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
 
一比一原版(CU毕业证)卡尔顿大学毕业证如何办理
一比一原版(CU毕业证)卡尔顿大学毕业证如何办理一比一原版(CU毕业证)卡尔顿大学毕业证如何办理
一比一原版(CU毕业证)卡尔顿大学毕业证如何办理
 
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
 
UofT毕业证如何办理
UofT毕业证如何办理UofT毕业证如何办理
UofT毕业证如何办理
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
 
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
 
Monthly Management report for the Month of May 2024
Monthly Management report for the Month of May 2024Monthly Management report for the Month of May 2024
Monthly Management report for the Month of May 2024
 

pure virtual function and abstract classes.ppt

  • 1.
  • 4. Virtual Functions – Define a virtual function in the base class. The word virtual appears only in the base class – Virtual function in base class stays virtual in all the derived classes – It can be overridden in the derived classes – But, a derived class is not required to re- implement a virtual function. If it does not, the base class version is used
  • 5. Static Binding • Static binding is the compile-time determination of which function to call for a particular object based on the type of the formal parameter • When pass-by-value is used, static binding occurs
  • 6. Dynamic Binding • Is the run-time determination of which function to call for a particular object of a derived class based on the type of the argument • Declaring a member function to be virtual instructs the compiler to generate code that guarantees dynamic binding • Dynamic binding requires pass-by- reference
  • 7. Pure Virtual Function • A pure virtual function is a virtual member function of a base class that must be overridden. • A pure virtual function is a virtual member function declared in a manner similar to the following: virtual void showInfo(void) = 0;
  • 8. Abstract Class • A class becomes an abstract base class when it contains one or more pure virtual functions. • An abstract base class is not instantiated, but other classes are derived from it.
  • 9. Abstract Classes & Pure Virtual Functions class Shape //Abstract { public : //Pure virtual Function virtual void draw() = 0; } • A class with one or more pure virtual functions is an Abstract Class • Objects of abstract class can’t be created Shape s; // error : variable of an abstract class • Some classes exist logically but not physically. • Example : Shape – Shape s; – Shape makes sense only as a base of some classes derived from it. Serves as a “category” – Hence instantiation of such a class must be prevented
  • 10. Example 10 Shape virtual void draw() Circle public void draw() Triangle public void draw()
  • 11. 11 • A pure virtual function not defined in the derived class remains a pure virtual function. • Hence derived class also becomes abstract class Circle : public Shape { //No draw() - Abstract public : void print(){ cout << “I am a circle” << endl; } class Rectangle : public Shape { public : void draw(){ // Override Shape::draw() cout << “Drawing Rectangle” << endl; } Rectangle r; // Valid Circle c; // error : variable of an abstract class
  • 12. Pure Virtual Functions: Summary • Pure virtual functions are useful because they make explicit the abstractness of a class • Tell both the user and the compiler how it was intended to be used • Note : It is a good idea to keep the common code as close as possible to the root of you hierarchy 12