Abstract Classes and Interfaces in C++ - Tutorial

Welcome to this tutorial on abstract classes and interfaces in the C++ programming language. Abstract classes and interfaces are key concepts in object-oriented programming that allow you to define common behaviors and create contracts for classes. They provide a way to achieve abstraction and polymorphism in your code. In this tutorial, we will explore how to define abstract classes, create interfaces, and use pure virtual functions to achieve these goals.

1. Abstract Classes

An abstract class is a class that cannot be instantiated and is meant to be subclassed. It serves as a base class for other classes and defines common behaviors and attributes. Abstract classes often contain one or more pure virtual functions, which are functions without any implementation. Here's an example:

      class Shape
      {
          public:
              virtual double area() = 0;
              virtual double perimeter() = 0;
      };
  class Rectangle : public Shape
  {
      private:
          double length;
          double width;
      
      public:
          Rectangle(double len, double wid) : length(len), width(wid) {}
          double area() override
          {
              return length * width;
          }
          
          double perimeter() override
          {
              return 2 * (length + width);
          }
  };

In the example above, we define an abstract class `Shape` that contains two pure virtual functions: `area()` and `perimeter()`. These functions are meant to be implemented by derived classes. We then create a derived class `Rectangle` that inherits from `Shape` and provides implementations for the pure virtual functions.

2. Interfaces

An interface is a class that consists entirely of pure virtual functions. It defines a contract that other classes must follow by implementing all the pure virtual functions. Interfaces provide a way to achieve multiple inheritance of behavior without the complexities of multiple inheritance. Here's an example:

      class Printable
      {
          public:
              virtual void print() const = 0;
      };
  class Shape : public Printable
  {
      public:
          virtual double area() const = 0;
          virtual double perimeter() const = 0;
          void print() const override
          {
              std::cout << "Area: " << area() << std::endl;
              std::cout << "Perimeter: " << perimeter() << std::endl;
          }
  };

In this example, we define an interface `Printable` that contains one pure virtual function `print()`. The `Shape` class inherits from `Printable` and also contains pure virtual functions `area()` and `perimeter()`. It provides an implementation for the `print()` function, which is a requirement of the `Printable` interface.

3. Common Mistakes with Abstract Classes and Interfaces

  • Forgetting to override pure virtual functions in derived classes, resulting in compilation errors.
  • Attempting to instantiate an abstract class, which is not allowed.
  • Not providing an implementation for pure virtual functions in derived classes.

FAQs about Abstract Classes and Interfaces in C++

  1. What is the purpose of an abstract class in C++?

    An abstract class in C++ is meant to serve as a base class for other classes. It defines common behaviors and attributes that derived classes must implement. Abstract classes provide a way to achieve abstraction and define contracts for classes.

  2. What is the difference between an abstract class and an interface?

    An abstract class can contain member variables, concrete functions, and pure virtual functions. It can also provide default implementations for some functions. An interface, on the other hand, consists entirely of pure virtual functions and does not contain member variables or concrete functions.

  3. Can a class inherit from multiple abstract classes?

    No, C++ does not support multiple inheritance of abstract classes. However, a class can inherit from one abstract class and implement multiple interfaces.

  4. Can an abstract class have a constructor?

    Yes, an abstract class can have a constructor. However, since abstract classes cannot be instantiated, the constructor is typically used by derived classes to initialize the base class.

  5. Can an interface inherit from another interface?

    Yes, an interface can inherit from another interface. This allows you to create a hierarchy of interfaces, with each interface building upon the behavior defined by its parent interface.

Summary

In this tutorial, we explored the concepts of abstract classes and interfaces in C++. Abstract classes provide a way to define common behaviors and attributes, while interfaces define contracts for classes. We learned how to create abstract classes with pure virtual functions and how to derive classes from them. We also saw how to create interfaces consisting entirely of pure virtual functions and how to implement them in classes. We discussed common mistakes, such as forgetting to override pure virtual functions and attempting to instantiate abstract classes. By understanding abstract classes and interfaces, you can design more flexible and modular code in C++.