0% found this document useful (0 votes)
144 views

Pure Virtual Functions and Abstract Classes in C++

An abstract class in C++ contains at least one pure virtual function. A pure virtual function is declared with a function body of 0 and provides an interface for derived classes to implement. Abstract classes cannot be instantiated but pointers to them can be declared, and derived classes must implement all pure virtual functions or they will also be abstract. The example code demonstrates a base abstract class B with a pure virtual function s(), a derived class D implementing s(), and calling s() through a base class pointer.

Uploaded by

Pramod Choudhary
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views

Pure Virtual Functions and Abstract Classes in C++

An abstract class in C++ contains at least one pure virtual function. A pure virtual function is declared with a function body of 0 and provides an interface for derived classes to implement. Abstract classes cannot be instantiated but pointers to them can be declared, and derived classes must implement all pure virtual functions or they will also be abstract. The example code demonstrates a base abstract class B with a pure virtual function s(), a derived class D implementing s(), and calling s() through a base class pointer.

Uploaded by

Pramod Choudhary
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

12/6/21, 2:42 PM Pure Virtual Functions and Abstract Classes in C++

Pure Virtual Functions and Abstract Classes in C++

A pure virtual function is a virtual function in C++ for which we need not to write any function
definition and only we have to declare it. It is declared by assigning 0 in the declaration.

An abstract class is a class in C++ which have at least one pure virtual function.

Abstract class can have normal functions and variables along with a pure virtual
function.

Abstract class cannot be instantiated, but pointers and references of Abstract class type
can be created.

Abstract classes are mainly used for Upcasting, so that its derived classes can use its
interface.

If an Abstract Class has derived class, they must implement all pure virtual functions, or
else they will become Abstract too.

We can’t create object of abstract class as we reserve a slot for a pure virtual function in
Vtable, but we don’t put any address, so Vtable will remain incomplete.

Example Code
 Live Demo
#include<iostream>

using namespace std;

class B {

   public:

      virtual void s() = 0; // Pure Virtual Function

};

class D:public B {

   public:

      void s() {

         cout << "Virtual Function in Derived class\n";

      }

};

int main() {

   B *b;

   D dobj;

   b = &dobj;

   b->s();

Output

Virtual Function in Derived class


https://www.tutorialspoint.com/pure-virtual-functions-and-abstract-classes-in-cplusplus 1/2

You might also like