Pure Virtual Functions and Abstract Classes in C++


In C++, pure virtual functions and abstract classes are key concepts used to implement abstraction and achieve runtime polymorphism. A pure virtual function is a function that is declared in a base class and has no definition in that class. Abstract classes are classes that contain at least one pure virtual function and cannot be instantiated directly. These features allow you to define interfaces for derived classes, enforcing them to provide their own implementations of the pure virtual functions.

Pure Virtual Function

A pure virtual function is a function that is declared in a base class but has no implementation. It is made pure by assigning `= 0` to the function declaration. This makes the base class abstract and prevents it from being instantiated. Any derived class must override and provide an implementation for the pure virtual function in order to be instantiated.

Syntax for Pure Virtual Function

    class Base {
    public:
        virtual void functionName() = 0;  // Pure virtual function
    };
        

Example of Pure Virtual Function

    #include <iostream>

    class Shape {
    public:
        virtual void draw() = 0;  // Pure virtual function
    };

    class Circle : public Shape {
    public:
        void draw() override {
            std::cout << "Drawing a Circle" << std::endl;
        }
    };

    class Rectangle : public Shape {
    public:
        void draw() override {
            std::cout << "Drawing a Rectangle" << std::endl;
        }
    };

    int main() {
        Shape* shape1 = new Circle();
        Shape* shape2 = new Rectangle();
        
        shape1->draw();  // Calls Circle's draw() method
        shape2->draw();  // Calls Rectangle's draw() method

        delete shape1;
        delete shape2;
        return 0;
    }
        

Output:

    Drawing a Circle
    Drawing a Rectangle
        

Explanation

In this example, the class Shape has a pure virtual function draw(). The derived classes Circle and Rectangle override this function with their own implementations. The base class Shape cannot be instantiated directly, and objects can only be created from classes that override the pure virtual function. When the draw() function is called through base class pointers, the correct function is chosen based on the actual object type (runtime polymorphism).

Abstract Classes

An abstract class in C++ is a class that contains at least one pure virtual function. Abstract classes cannot be instantiated directly because they are incomplete and are meant to serve as base classes for derived classes that implement the pure virtual functions.

Syntax for Abstract Class

    class AbstractClass {
    public:
        virtual void pureVirtualFunction() = 0;  // Pure virtual function
    };
        

Abstract classes are used to define common interfaces or templates for derived classes, ensuring that the derived classes implement certain functionality.

Example of Abstract Class

    #include <iostream>

    class Animal {
    public:
        virtual void sound() = 0;  // Pure virtual function makes this class abstract
    };

    class Dog : public Animal {
    public:
        void sound() override {
            std::cout << "Dog barks" << std::endl;
        }
    };

    class Cat : public Animal {
    public:
        void sound() override {
            std::cout << "Cat meows" << std::endl;
        }
    };

    int main() {
        Animal* animal1 = new Dog();
        Animal* animal2 = new Cat();
        
        animal1->sound();  // Calls Dog's sound() method
        animal2->sound();  // Calls Cat's sound() method

        delete animal1;
        delete animal2;
        return 0;
    }
        

Output:

    Dog barks
    Cat meows
        

Explanation

In this example, the class Animal is an abstract class because it contains the pure virtual function sound(). The derived classes Dog and Cat implement the sound() function, making them concrete classes that can be instantiated. The base class Animal cannot be instantiated because it is abstract.

Why Use Pure Virtual Functions and Abstract Classes?

Pure virtual functions and abstract classes are used in C++ to achieve abstraction. They allow you to define interfaces that must be implemented by derived classes, ensuring consistency across different derived classes. Abstract classes provide a way to structure code and define common functionality while leaving specific details to be implemented by derived classes.

Advantages

  • Code Reusability: Abstract classes allow common functionality to be defined once in the base class and reused in derived classes.
  • Enforced Interface: Using pure virtual functions forces derived classes to implement the required methods, ensuring consistency in behavior.
  • Flexibility: Abstract classes provide a flexible way to design systems where different derived classes can share a common interface but implement their own specific behavior.

Conclusion

Pure virtual functions and abstract classes are essential components of object-oriented programming in C++. They help define interfaces that derived classes must implement and allow for flexible, extensible designs. By using pure virtual functions, you can create a class that provides a template for other classes, ensuring that certain functions are implemented while allowing derived classes to provide their own specific behavior.





Advertisement