C++ Abstraction

Abstraction is one of the core principles of object-oriented programming (OOP) that focuses on hiding the implementation details and showing only the essential features of an object. It allows a programmer to manage complexity by providing a simple interface to interact with complex systems. In essence, abstraction lets you work with high-level concepts while hiding the low-level details.

Key Concepts of Abstraction

1. Purpose of Abstraction:

• Simplifies Interaction: Provides a simpler interface for the user, hiding the internal workings and complexities of the system.

• Reduces Complexity: By focusing on high-level operations and ignoring the low-level implementation details, abstraction helps manage complexity.

2. Abstraction vs. Encapsulation:

• Abstraction is about hiding implementation details and showing only essential features.

• Encapsulation involves bundling the data and methods that operate on the data within a single unit or class, and restricting access to some of the object's components.

3. Implementing Abstraction in C++:

• Abstract Classes: An abstract class is a class that cannot be instantiated on its own and is designed to be inherited by other classes. It can have pure virtual functions that must be implemented by derived classes.

• Pure Virtual Functions: A pure virtual function is a function that has no implementation in the base class and must be implemented by derived classes. It is declared using = 0 in the class definition.

Example of Abstraction in C++

Consider an abstract class Shape that defines a common interface for different shapes but does not provide a concrete implementation for drawing the shape.

Derived classes like Circle and Rectangle will provide their specific implementations.

Example:

<#include <iostream>>

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

    // Virtual destructor
    virtual ~Shape() {}
};

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

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

// Function to use abstraction
void displayDrawing(const Shape& shape) {
  shape.draw();
}

int main() {
  Circle c;
  Rectangle r;

  displayDrawing(c); // Output: Drawing a circle
  displayDrawing(r); // Output: Drawing a rectangle

  return 0;
}

Output:

Drawing a circle
Drawing a rectangle

Important Points About Abstraction

1. Abstract Classes:

• Abstract classes cannot be instantiated. They are meant to be base classes.

• They can have both pure virtual functions (which must be implemented by derived classes) and regular methods with implementation.

2. Pure Virtual Functions:

• Declared by assigning = 0 in the class definition.

• Forces derived classes to provide an implementation.

3. Virtual Destructors:

• It’s good practice to declare a virtual destructor in an abstract class to ensure that derived class destructors are called when deleting through a base class pointer.

4. Interfaces:

• In C++, an abstract class with only pure virtual functions is often referred to as an interface. It provides a way to define a contract that derived classes must adhere to.