codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

54 / 87 topics
48Inheritance Basics49Public, Protected, and Private Inheritance50Types of Inheritance51Polymorphism52Function Overriding53Virtual Functions54Abstract Class and Pure Virtual Function
Tutorials/C++ Programming/Abstract Class and Pure Virtual Function
⚡C++ Programming

Abstract Class and Pure Virtual Function

Updated 2026-05-12
30 min read

Abstract Class and Pure Virtual Function

In object-oriented programming, abstract classes and pure virtual functions are powerful tools that help define interfaces and enforce polymorphism. An abstract class is a class that cannot be instantiated on its own and must be inherited by other classes. A pure virtual function is a virtual function that has no implementation in the base class and must be overridden by derived classes.

Understanding abstract classes and pure virtual functions is crucial for designing flexible and reusable code, especially when implementing interfaces or defining a contract that derived classes must adhere to. In this tutorial, we will explore the syntax of pure virtual functions, how they define abstract classes, and how you can use them in C++ programming.

Introduction

Abstract classes and pure virtual functions are fundamental concepts in C++ that enable polymorphism and interface design. By using these features, you can create a base class with some methods that must be implemented by any derived class, ensuring a consistent API across different implementations.

This tutorial will guide you through the syntax of pure virtual functions, how to declare abstract classes, and provide practical examples to illustrate their use in C++ programming.

Pure Virtual Function Syntax

A pure virtual function is declared by assigning 0 in its declaration within a class. This indicates that the function has no implementation in the base class and must be overridden by any non-abstract derived class.

Syntax

C++
1class Base {
2public:
3 virtual void pureVirtualFunction() = 0; // Pure virtual function
4};

In this example, pureVirtualFunction is a pure virtual function. The = 0 syntax makes the Base class an abstract class.

Abstract Class

An abstract class is a class that contains at least one pure virtual function. It cannot be instantiated directly; you must inherit from it and provide implementations for all its pure virtual functions in the derived classes.

Example

Let's create an abstract class called Shape with a pure virtual function area():

C++
1class Shape {
2public:
3 virtual double area() = 0; // Pure virtual function
4};

In this example, Shape is an abstract class because it contains the pure virtual function area(). You cannot create an instance of Shape directly.

Interface Pattern in C++

The interface pattern involves defining an abstract class with only pure virtual functions. This abstract class serves as a contract that derived classes must implement. The interface pattern is useful for defining a set of methods that all implementations must provide, ensuring consistency across different types.

Example

Let's define an Animal interface with two pure virtual functions: speak() and move():

C++
1class Animal {
2public:
3 virtual void speak() = 0; // Pure virtual function
4 virtual void move() = 0; // Pure virtual function
5};

In this example, Animal is an interface that defines the methods speak() and move(). Any class inheriting from Animal must implement these methods.

Concrete Class

A concrete class is a class that is not abstract and can be instantiated. It provides implementations for all its virtual functions, including pure virtual functions inherited from base classes.

Example

Let's create a concrete class called Dog that inherits from the Animal interface:

C++
1class Dog : public Animal {
2public:
3 void speak() override {
4 std::cout << "Woof!" << std::endl;
5 }
6
7 void move() override {
8 std::cout << "Running" << std::endl;
9 }
10};

In this example, Dog is a concrete class that implements the speak() and move() methods inherited from the Animal interface.

Practical Example

Let's create a complete program that demonstrates the use of abstract classes and pure virtual functions. We'll define an Animal interface and two derived classes: Dog and Cat.

C++
1#include <iostream>
2
3// Abstract class with pure virtual functions
4class Animal {
5public:
6 virtual void speak() = 0; // Pure virtual function
7 virtual void move() = 0; // Pure virtual function
8};
9
10// Concrete class implementing the Animal interface
11class Dog : public Animal {
12public:
13 void speak() override {
14 std::cout << "Woof!" << std::endl;
15 }
16
17 void move() override {
18 std::cout << "Running" << std::endl;
19 }
20};
21
22// Another concrete class implementing the Animal interface
23class Cat : public Animal {
24public:
25 void speak() override {
26 std::cout << "Meow!" << std::endl;
27 }
28
29 void move() override {
30 std::cout << "Jumping" << std::endl;
31 }
32};
33
34int main() {
35 Dog dog;
36 Cat cat;
37
38 dog.speak();
39 dog.move();
40
41 cat.speak();
42 cat.move();
43
44 return 0;
45}

Output

Output
Woof!
Running
Meow!
Jumping

In this example, we define an Animal interface with two pure virtual functions: speak() and move(). We then create two concrete classes, Dog and Cat, that implement these methods. The main function creates instances of Dog and Cat and calls their methods.

Summary

  • Pure Virtual Function: A virtual function declared with = 0 in a class.
  • Abstract Class: A class containing at least one pure virtual function; cannot be instantiated directly.
  • Interface Pattern: An abstract class used to define a contract that derived classes must implement.
  • Concrete Class: A class that provides implementations for all its virtual functions and can be instantiated.

What's Next?

In the next topic, we will explore the new features introduced in C++11. These features include auto, lambda expressions, range-based for loops, and more, which enhance the language's capabilities and make programming easier and more efficient. Stay tuned!


PreviousVirtual FunctionsNext C++11 Features

Recommended Gear

Virtual FunctionsC++11 Features