際際滷

際際滷Share a Scribd company logo
pure virtual function and abstract classes.ppt
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 cant 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 (20)

Inheritance, Polymorphism, and Virtual Functions (Ch_15).pptx
Inheritance, Polymorphism, and Virtual Functions (Ch_15).pptxInheritance, Polymorphism, and Virtual Functions (Ch_15).pptx
Inheritance, Polymorphism, and Virtual Functions (Ch_15).pptx
nasiruni6428
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
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
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
Inheritance, Polymorphism, and Virtual Functions (Ch_15).pptx
Inheritance, Polymorphism, and Virtual Functions (Ch_15).pptxInheritance, Polymorphism, and Virtual Functions (Ch_15).pptx
Inheritance, Polymorphism, and Virtual Functions (Ch_15).pptx
nasiruni6428
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
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
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
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
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

Recently uploaded (20)

Optimizing Common Table Expressions in Apache Hive with Calcite
Optimizing Common Table Expressions in Apache Hive with CalciteOptimizing Common Table Expressions in Apache Hive with Calcite
Optimizing Common Table Expressions in Apache Hive with Calcite
Stamatis Zampetakis
Monitoring Imam Ririn di Pilkada Kota Depok 2024
Monitoring Imam Ririn di Pilkada Kota Depok 2024Monitoring Imam Ririn di Pilkada Kota Depok 2024
Monitoring Imam Ririn di Pilkada Kota Depok 2024
Deddy Rahman
CH. 4.pptxt and I will be there in about
CH. 4.pptxt and I will be there in aboutCH. 4.pptxt and I will be there in about
CH. 4.pptxt and I will be there in about
miesoabdela57
"MIAO Ecosystem Financial Management PPT
"MIAO Ecosystem Financial Management PPT"MIAO Ecosystem Financial Management PPT
"MIAO Ecosystem Financial Management PPT
miao22
Analyzing Consumer Spending Trends and Purchasing Behavior
Analyzing Consumer Spending Trends and Purchasing BehaviorAnalyzing Consumer Spending Trends and Purchasing Behavior
Analyzing Consumer Spending Trends and Purchasing Behavior
omololaokeowo1
Deep-QPP: A Pairwise Interaction-based Deep Learning Model for Supervised Que...
Deep-QPP: A Pairwise Interaction-based Deep Learning Model for Supervised Que...Deep-QPP: A Pairwise Interaction-based Deep Learning Model for Supervised Que...
Deep-QPP: A Pairwise Interaction-based Deep Learning Model for Supervised Que...
suchanadatta3
2025-03-03-Philly-AAAI-GoodData-Build Secure RAG Apps With Open LLM
2025-03-03-Philly-AAAI-GoodData-Build Secure RAG Apps With Open LLM2025-03-03-Philly-AAAI-GoodData-Build Secure RAG Apps With Open LLM
2025-03-03-Philly-AAAI-GoodData-Build Secure RAG Apps With Open LLM
Timothy Spann
Lecture-AI and Alogor Parallel Aglorithms.pptx
Lecture-AI and Alogor Parallel Aglorithms.pptxLecture-AI and Alogor Parallel Aglorithms.pptx
Lecture-AI and Alogor Parallel Aglorithms.pptx
humairafatima22
Introduction Lecture 01 Data Science.pdf
Introduction Lecture 01 Data Science.pdfIntroduction Lecture 01 Data Science.pdf
Introduction Lecture 01 Data Science.pdf
messagetome133
MTC Supply Chain Management Strategy.pptx
MTC Supply Chain Management Strategy.pptxMTC Supply Chain Management Strategy.pptx
MTC Supply Chain Management Strategy.pptx
Rakshit Porwal
stages-of-moral-development-lawrence-kohlberg-pdf-free.pdf
stages-of-moral-development-lawrence-kohlberg-pdf-free.pdfstages-of-moral-development-lawrence-kohlberg-pdf-free.pdf
stages-of-moral-development-lawrence-kohlberg-pdf-free.pdf
esguerramark1991
The Marketability of Rice Straw Yarn Among Selected Customers of Gantsilyo Guru
The Marketability of Rice Straw Yarn Among Selected Customers of Gantsilyo GuruThe Marketability of Rice Straw Yarn Among Selected Customers of Gantsilyo Guru
The Marketability of Rice Straw Yarn Among Selected Customers of Gantsilyo Guru
kenyoncenteno12
Class 3-Workforce profile updated P.pptx
Class 3-Workforce profile updated P.pptxClass 3-Workforce profile updated P.pptx
Class 3-Workforce profile updated P.pptx
angelananalucky
Lesson 9- Data Governance and Ethics.pptx
Lesson 9- Data Governance and Ethics.pptxLesson 9- Data Governance and Ethics.pptx
Lesson 9- Data Governance and Ethics.pptx
1045858
iam free indeed.pptxiam free indeed.pptx
iam free indeed.pptxiam free indeed.pptxiam free indeed.pptxiam free indeed.pptx
iam free indeed.pptxiam free indeed.pptx
muhweziart
Stasiun kernel pabrik kelapa sawit indonesia
Stasiun kernel pabrik kelapa sawit indonesiaStasiun kernel pabrik kelapa sawit indonesia
Stasiun kernel pabrik kelapa sawit indonesia
fikrimanurung1
HIRE MUYERN TRUST HACKER FOR AUTHENTIC CYBER SERVICES
HIRE MUYERN TRUST HACKER FOR AUTHENTIC CYBER SERVICESHIRE MUYERN TRUST HACKER FOR AUTHENTIC CYBER SERVICES
HIRE MUYERN TRUST HACKER FOR AUTHENTIC CYBER SERVICES
anastasiapenova16
CloudMonitor - Architecture Audit Review February 2025.pdf
CloudMonitor - Architecture Audit Review February 2025.pdfCloudMonitor - Architecture Audit Review February 2025.pdf
CloudMonitor - Architecture Audit Review February 2025.pdf
Rodney Joyce
vnptloveeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
vnptloveeeeeeeeeeeeeeeeeeeeeeeeeeee.pptxvnptloveeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
vnptloveeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
deomom129
Data-Models-in-DBMS-An-Overview.pptx.pptx
Data-Models-in-DBMS-An-Overview.pptx.pptxData-Models-in-DBMS-An-Overview.pptx.pptx
Data-Models-in-DBMS-An-Overview.pptx.pptx
hfebxtveyjxavhx
Optimizing Common Table Expressions in Apache Hive with Calcite
Optimizing Common Table Expressions in Apache Hive with CalciteOptimizing Common Table Expressions in Apache Hive with Calcite
Optimizing Common Table Expressions in Apache Hive with Calcite
Stamatis Zampetakis
Monitoring Imam Ririn di Pilkada Kota Depok 2024
Monitoring Imam Ririn di Pilkada Kota Depok 2024Monitoring Imam Ririn di Pilkada Kota Depok 2024
Monitoring Imam Ririn di Pilkada Kota Depok 2024
Deddy Rahman
CH. 4.pptxt and I will be there in about
CH. 4.pptxt and I will be there in aboutCH. 4.pptxt and I will be there in about
CH. 4.pptxt and I will be there in about
miesoabdela57
"MIAO Ecosystem Financial Management PPT
"MIAO Ecosystem Financial Management PPT"MIAO Ecosystem Financial Management PPT
"MIAO Ecosystem Financial Management PPT
miao22
Analyzing Consumer Spending Trends and Purchasing Behavior
Analyzing Consumer Spending Trends and Purchasing BehaviorAnalyzing Consumer Spending Trends and Purchasing Behavior
Analyzing Consumer Spending Trends and Purchasing Behavior
omololaokeowo1
Deep-QPP: A Pairwise Interaction-based Deep Learning Model for Supervised Que...
Deep-QPP: A Pairwise Interaction-based Deep Learning Model for Supervised Que...Deep-QPP: A Pairwise Interaction-based Deep Learning Model for Supervised Que...
Deep-QPP: A Pairwise Interaction-based Deep Learning Model for Supervised Que...
suchanadatta3
2025-03-03-Philly-AAAI-GoodData-Build Secure RAG Apps With Open LLM
2025-03-03-Philly-AAAI-GoodData-Build Secure RAG Apps With Open LLM2025-03-03-Philly-AAAI-GoodData-Build Secure RAG Apps With Open LLM
2025-03-03-Philly-AAAI-GoodData-Build Secure RAG Apps With Open LLM
Timothy Spann
Lecture-AI and Alogor Parallel Aglorithms.pptx
Lecture-AI and Alogor Parallel Aglorithms.pptxLecture-AI and Alogor Parallel Aglorithms.pptx
Lecture-AI and Alogor Parallel Aglorithms.pptx
humairafatima22
Introduction Lecture 01 Data Science.pdf
Introduction Lecture 01 Data Science.pdfIntroduction Lecture 01 Data Science.pdf
Introduction Lecture 01 Data Science.pdf
messagetome133
MTC Supply Chain Management Strategy.pptx
MTC Supply Chain Management Strategy.pptxMTC Supply Chain Management Strategy.pptx
MTC Supply Chain Management Strategy.pptx
Rakshit Porwal
stages-of-moral-development-lawrence-kohlberg-pdf-free.pdf
stages-of-moral-development-lawrence-kohlberg-pdf-free.pdfstages-of-moral-development-lawrence-kohlberg-pdf-free.pdf
stages-of-moral-development-lawrence-kohlberg-pdf-free.pdf
esguerramark1991
The Marketability of Rice Straw Yarn Among Selected Customers of Gantsilyo Guru
The Marketability of Rice Straw Yarn Among Selected Customers of Gantsilyo GuruThe Marketability of Rice Straw Yarn Among Selected Customers of Gantsilyo Guru
The Marketability of Rice Straw Yarn Among Selected Customers of Gantsilyo Guru
kenyoncenteno12
Class 3-Workforce profile updated P.pptx
Class 3-Workforce profile updated P.pptxClass 3-Workforce profile updated P.pptx
Class 3-Workforce profile updated P.pptx
angelananalucky
Lesson 9- Data Governance and Ethics.pptx
Lesson 9- Data Governance and Ethics.pptxLesson 9- Data Governance and Ethics.pptx
Lesson 9- Data Governance and Ethics.pptx
1045858
iam free indeed.pptxiam free indeed.pptx
iam free indeed.pptxiam free indeed.pptxiam free indeed.pptxiam free indeed.pptx
iam free indeed.pptxiam free indeed.pptx
muhweziart
Stasiun kernel pabrik kelapa sawit indonesia
Stasiun kernel pabrik kelapa sawit indonesiaStasiun kernel pabrik kelapa sawit indonesia
Stasiun kernel pabrik kelapa sawit indonesia
fikrimanurung1
HIRE MUYERN TRUST HACKER FOR AUTHENTIC CYBER SERVICES
HIRE MUYERN TRUST HACKER FOR AUTHENTIC CYBER SERVICESHIRE MUYERN TRUST HACKER FOR AUTHENTIC CYBER SERVICES
HIRE MUYERN TRUST HACKER FOR AUTHENTIC CYBER SERVICES
anastasiapenova16
CloudMonitor - Architecture Audit Review February 2025.pdf
CloudMonitor - Architecture Audit Review February 2025.pdfCloudMonitor - Architecture Audit Review February 2025.pdf
CloudMonitor - Architecture Audit Review February 2025.pdf
Rodney Joyce
vnptloveeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
vnptloveeeeeeeeeeeeeeeeeeeeeeeeeeee.pptxvnptloveeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
vnptloveeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
deomom129
Data-Models-in-DBMS-An-Overview.pptx.pptx
Data-Models-in-DBMS-An-Overview.pptx.pptxData-Models-in-DBMS-An-Overview.pptx.pptx
Data-Models-in-DBMS-An-Overview.pptx.pptx
hfebxtveyjxavhx

pure virtual function and abstract classes.ppt

  • 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 cant 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