Pure Virtual and Abstract
Date: 31st August 2020
By: Amit Kumar Trivedi
KHYATI SCHOOL OF COMPUTER APPLICATION
Introduction
Rules for virtual function
Internals of Virtual Functions
Pure virtual function
Virtual Base class
Virtual destructor
Abstract class
Limitations of virtual Function
Early binding v /s Late binding
Schedule
Start at : 10:07am
Break at 11 am for 7 minutes
Close at 11:50am
Rules for virtual function
•Virtual functions must be members of some class.
•Virtual functions cannot be static members.
•They are accessed through object pointers.
•They can be a friend of another class.
•A virtual function must be defined in the base
class, even though it is not used.
Rules for virtual function
•The prototypes of a virtual function of the base
class and all the derived classes must be identical. If
the two functions with the same name but different
prototypes, C++ will consider them as the
overloaded functions.
•We cannot have a virtual constructor, but we can
have a virtual destructor
Pure virtual function
Pure virtual function can be defined as:
virtual void display() = 0;
Pure virtual function
A virtual function is not used for performing any task. It only serves as a
placeholder.
When the function has no definition, such function is known as "do-nothing"
function.
The "do-nothing" function is known as a pure virtual function.
A pure virtual function is a function declared in the base class that has no
definition relative to the base class.
A class containing the pure virtual function cannot be used to declare the
objects of its own, such classes are known as abstract base classes.
The main objective of the base class is to provide the traits to the derived
classes and to create the base pointer used for achieving the runtime
polymorphism.
Virtual Function
Virtual Base class
Dimond Problem
Abstract class
Abstract Class is a class which contains atleast 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.
Limitations of virtual
Function
Practice
Create a Class Vehicle. Derived Class Car and
Truck. Chesis number is property of Vehicle class.
Function Drive.
I am driving car.
I am driving truck.
Demonstrate Dimond problem using this and
solution of it.
Make Drive pure virtual and Vechile as abstract
class
Early binding v /s Late
binding
Early Binding Late Bindning
Function
Overloading
Function
Overriding
Compile Time Run Time
Questions
Thank you

Pure virtual function and abstract class

  • 1.
    Pure Virtual andAbstract Date: 31st August 2020 By: Amit Kumar Trivedi KHYATI SCHOOL OF COMPUTER APPLICATION
  • 2.
    Introduction Rules for virtualfunction Internals of Virtual Functions Pure virtual function Virtual Base class Virtual destructor Abstract class Limitations of virtual Function Early binding v /s Late binding
  • 3.
    Schedule Start at :10:07am Break at 11 am for 7 minutes Close at 11:50am
  • 4.
    Rules for virtualfunction •Virtual functions must be members of some class. •Virtual functions cannot be static members. •They are accessed through object pointers. •They can be a friend of another class. •A virtual function must be defined in the base class, even though it is not used.
  • 5.
    Rules for virtualfunction •The prototypes of a virtual function of the base class and all the derived classes must be identical. If the two functions with the same name but different prototypes, C++ will consider them as the overloaded functions. •We cannot have a virtual constructor, but we can have a virtual destructor
  • 6.
    Pure virtual function Purevirtual function can be defined as: virtual void display() = 0;
  • 7.
    Pure virtual function Avirtual function is not used for performing any task. It only serves as a placeholder. When the function has no definition, such function is known as "do-nothing" function. The "do-nothing" function is known as a pure virtual function. A pure virtual function is a function declared in the base class that has no definition relative to the base class. A class containing the pure virtual function cannot be used to declare the objects of its own, such classes are known as abstract base classes. The main objective of the base class is to provide the traits to the derived classes and to create the base pointer used for achieving the runtime polymorphism.
  • 8.
  • 9.
  • 11.
    Abstract class Abstract Classis a class which contains atleast 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.
  • 13.
  • 14.
    Practice Create a ClassVehicle. Derived Class Car and Truck. Chesis number is property of Vehicle class. Function Drive. I am driving car. I am driving truck. Demonstrate Dimond problem using this and solution of it. Make Drive pure virtual and Vechile as abstract class
  • 15.
    Early binding v/s Late binding Early Binding Late Bindning Function Overloading Function Overriding Compile Time Run Time
  • 16.
  • 17.