C++ Cannot Create Instance Of Abstract Class

4 min read Jul 01, 2024
C++ Cannot Create Instance Of Abstract Class

C++ Cannot Create Instance of Abstract Class

In C++, an abstract class is a class that cannot be instantiated directly. This means you can't create an object of that class using the new keyword or by declaring a variable of that class type.

Why is this?

Because abstract classes are designed to be base classes for other classes. They act as blueprints that define common characteristics and behaviors for derived classes.

Here's a breakdown of the concept:

Why Abstract Classes Exist?

  • Encapsulation and Abstraction: Abstract classes help achieve encapsulation and abstraction by defining a common interface for a family of related classes, hiding implementation details.
  • Polymorphism: Abstract classes allow for polymorphism, where a single variable can hold objects of different derived classes, each implementing the abstract methods in their own way.
  • Code Reusability: By inheriting from an abstract class, derived classes automatically inherit the abstract methods, promoting code reusability and consistency.

Key Characteristics of Abstract Classes

  1. Abstract Methods: Abstract classes contain at least one abstract method. Abstract methods are declared but not defined, meaning they have no implementation within the abstract class. They are denoted by the keyword = 0 after the method declaration.
  2. No Instantiation: As mentioned earlier, you cannot create an instance of an abstract class. Trying to do so will result in a compile-time error.

Example:

#include 

// Abstract class Shape
class Shape {
public:
    // Pure virtual function - abstract method
    virtual double calculateArea() = 0;
};

// Derived class Circle
class Circle : public Shape {
public:
    Circle(double radius) : radius(radius) {}

    // Implementation of the abstract method
    double calculateArea() override {
        return 3.14159 * radius * radius;
    }

private:
    double radius;
};

int main() {
    // Error: cannot create an instance of Shape
    // Shape shape; 

    // Create an instance of Circle
    Circle circle(5.0);

    // Calculate and print area
    std::cout << "Area of Circle: " << circle.calculateArea() << std::endl;

    return 0;
}

Explanation:

  • The Shape class is an abstract class, containing an abstract method calculateArea().
  • The Circle class inherits from Shape and provides an implementation for the calculateArea() method.
  • You cannot create an object of Shape, but you can create an object of Circle.
  • The override keyword is optional but highly recommended to indicate that the method is overriding an abstract method.

Summary:

Abstract classes play a crucial role in object-oriented programming by enforcing a common interface for derived classes, promoting code reusability, and enabling polymorphism. While you cannot create instances of abstract classes, they provide a powerful framework for defining and implementing relationships between related classes.