
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
#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
- Related Articles
- Virtual functions in derived classes in C++
- What is the difference between virtual and abstract functions in C#?
- Difference Between Virtual and Pure Virtual Function
- Abstract Method and Classes in Java
- Abstract Classes in Java
- Abstract Classes in C#
- Abstract Classes in Dart Programming
- Difference between Traits and Abstract Classes in Scala.
- Pure virtual destructor in C++
- What are abstract classes in Java?
- What are abstract classes in C#?
- Abstract Base Classes in Python (abc)
- Difference between a virtual function and a pure virtual function in C++
- How to create abstract classes in TypeScript?
- Python Abstract Base Classes for Containers
