Abstract Classes in C++

Abstraction in object-oriented programming (OOP) is the concept of simplifying complex systems by representing them at a higher, more generalized level. It involves defining the essential characteristics and behavior of an object or class while hiding the implementation details. Abstraction enables developers to focus on the key aspects of an object, class, or system, making code more understandable and maintainable. By defining abstract classes or interfaces, OOP languages like C++ and Java allow for the creation of blueprints for classes, promoting modularity and code reusability, and supporting the development of complex systems with ease.

Characteristics of abstract class in C++

Abstract classes in C++ possess the following characteristics:

  1. Declaring Pure Virtual Functions

    Abstract classes contain one or more pure virtual functions, which are declared with the virtual keyword and have no implementation. These functions serve as placeholders for concrete (overridden) functions in derived classes.
  2. Inability to Instantiate

    You cannot create instances (objects) of an abstract class. Attempting to do so results in a compilation error. Abstract classes are meant to be base classes for other classes.
  3. Derived Class Implementation

    Derived classes that inherit from an abstract class must provide concrete implementations for all the pure virtual functions declared in the abstract class. Failure to do so will make the derived class abstract as well.
  4. Can Contain Data Members and Concrete Member Functions

    Abstract classes can contain data members, concrete member functions (i.e., regular functions with implementations), and other characteristics typical of C++ classes.
  5. Serve as Interfaces

    Abstract classes often serve as interfaces, defining a contract that derived classes must adhere to. They provide a common structure and behavior that ensures uniformity and consistency among various derived classes.
  6. Support Polymorphism

    Abstract classes facilitate polymorphism by allowing objects of different derived classes to be treated as instances of the abstract base class. This enables the use of pointers or references to the abstract base class for dynamic method dispatch.
  7. May Contain Constructors and Destructors

    Abstract classes can have constructors and destructors, but they are typically protected or private. These constructors and destructors are invoked when objects of derived classes are created and destroyed.

Here's an example of an abstract class in C++:

#include <iostream> // Abstract class class Shape { public: // Pure virtual function (no implementation) virtual double area() const = 0; // Regular function with implementation void printDescription() const { std::cout << "This is a shape." << std::endl; } }; // Derived class class Circle : public Shape { private: double radius; public: Circle(double r) : radius(r) {} // Implementation of the pure virtual function double area() const override { return 3.14159 * radius * radius; } void printDescription() const { std::cout << "This is a circle." << std::endl; } }; // Derived class class Square : public Shape { private: double side; public: Square(double s) : side(s) {} // Implementation of the pure virtual function double area() const override { return side * side; } }; int main() { // Shape shape; // Error: Cannot create an instance of an abstract class Circle circle(5.0); Square square(4.0); // Use base class pointers to achieve polymorphism Shape* shapes[] = {&circle, &square}; for (const auto shape : shapes) { shape->printDescription(); std::cout << "Area: " << shape->area() << std::endl; } return 0; }

In this example, the Shape class is an abstract class with a pure virtual function area(), which is a common interface for calculating the area of shapes. Derived classes Circle and Square inherit from the Shape class and provide concrete implementations of the area() function. While the Shape class cannot be instantiated, you can create instances of the derived classes and use base class pointers to achieve polymorphism, demonstrating the use of an abstract class to define a common interface for various shapes.

Pure Virtual Functions and Abstract classes

pure virtual functions and abstract classes are related concepts used to achieve abstraction and provide a common interface for derived classes. Here's an explanation of each:

Pure Virtual Functions

A pure virtual function is a virtual function declared in a base class with the = 0 syntax at the end of its declaration. It has no implementation in the base class and is meant to be overridden by derived classes. Pure virtual functions serve as placeholders, defining a function that must be implemented by any class derived from the base class.

Attempting to create an instance of a class containing a pure virtual function results in a compilation error, making it impossible to instantiate an object of that class. Pure virtual functions enforce a contract that derived classes must follow.

Abstract Classes

An abstract class is a class that contains one or more pure virtual functions. These classes are not meant to be instantiated directly but rather serve as base classes for other classes. Abstract classes define a common interface or structure that derived classes should adhere to.

Derived classes inheriting from an abstract class must provide concrete implementations for all the pure virtual functions declared in the abstract class. This ensures that every derived class adheres to the specified interface.

Here's an example that illustrates both pure virtual functions and abstract classes:

#include <iostream> class Shape { public: // Pure virtual function virtual double area() const = 0; }; class Circle : public Shape { private: double radius; public: Circle(double r) : radius(r) {} // Implementation of the pure virtual function double area() const override { return 3.14159 * radius * radius; } }; int main() { // Shape shape; // Error: Cannot create an instance of an abstract class Circle circle(5.0); std::cout << "Area of the circle: " << circle.area() << std::endl; return 0; }

Why Can't Make an Abstract Class Object?

You cannot create an object of an abstract class in C++ because abstract classes contain one or more pure virtual functions, which have no implementation in the base class. An abstract class serves as a blueprint for derived classes, defining a common interface that must be implemented by any class inheriting from it. Attempting to create an object of an abstract class would be ambiguous because there are unimplemented functions in the class, and it wouldn't make sense to use an object with undefined behavior. The purpose of an abstract class is to provide a structure and interface for derived classes, not to be instantiated itself.

What are the restrictions to abstract class?

Abstract classes are a way to define a class that cannot be instantiated and is meant to be used as a base class for other classes. They are typically used in conjunction with pure virtual functions to create an interface for derived classes. Here are some important restrictions and characteristics of abstract classes in C++:

  1. Cannot Be Instantiated: Objects of an abstract class cannot be created directly. Attempting to do so will result in a compilation error. Abstract classes are meant to be subclassed and serve as a blueprint for derived classes.
  2. Pure Virtual Functions: Abstract classes often contain one or more pure virtual functions. A pure virtual function is declared with "= 0" and has no implementation in the base class. Derived classes must provide implementations for all pure virtual functions, or they will also be considered abstract.
  3. Derived Class Responsibility: Abstract classes are meant to be subclassed. Any class that derives from an abstract class must provide implementations for all pure virtual functions defined in the base class.
  4. Can Have Concrete Member Functions: Abstract classes can have regular (concrete) member functions with implementations. These functions can be used to provide common functionality that derived classes may share.
  5. Can Have Data Members: Abstract classes can have data members (variables) just like any other class. These data members can be used to store shared data for derived classes.
  6. Polymorphism: Abstract classes and pure virtual functions play a significant role in achieving polymorphism in C++. Polymorphism allows you to treat derived objects as objects of the base class, providing a level of abstraction and flexibility in your code.
  7. Cannot Create Objects of Abstract Class: You cannot create instances of an abstract class. You can only create instances of classes derived from the abstract class. This is because abstract classes are incomplete and lack implementations for pure virtual functions.

Conclusion

An abstract class in C++ is a class that cannot be instantiated and is meant to be subclassed. It often includes pure virtual functions, which must be implemented by derived classes, and it serves as a blueprint for creating related classes with a common interface.