Yes, you absolutely can create abstract classes.
Abstract classes serve as blueprints for other classes, defining a common interface but often leaving the implementation of certain functionalities to their derived classes. You cannot directly create objects (instances) of an abstract class.
How Abstract Classes Are Created
Based on the provided reference, you create an abstract class by declaring at least one pure virtual member function.
Here's the breakdown:
- Virtual Function: A function declared with the
virtual
keyword in a base class. This allows a function call to a base class pointer or reference to be resolved at runtime, based on the actual type of the object it points to. - Pure Specifier (
= 0
): When you declare a virtual function and immediately follow it with= 0
, you make it a pure virtual function. This indicates that the base class does not provide an implementation for this function, and any concrete (non-abstract) class derived from it must provide its own implementation. - Abstract Class Definition: A class becomes abstract as soon as it contains at least one pure virtual function.
Let's look at an example structure (using C++ syntax, as the reference implies):
class Shape { // Abstract class
public:
// Pure virtual function - makes Shape abstract
virtual double getArea() const = 0;
// Other virtual or non-virtual functions can exist
virtual void display() const {
// Default implementation
std::cout << "This is a shape." << std::endl;
}
// Destructor should generally be virtual in base classes
virtual ~Shape() {}
};
// A concrete class derived from Shape
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
// Must implement the pure virtual function getArea()
double getArea() const override {
return 3.14159 * radius * radius;
}
// Can optionally override other virtual functions
void display() const override {
std::cout << "This is a circle." << std::endl;
}
};
// This would be an abstract class too if it didn't implement getArea()
class Rectangle : public Shape {
private:
double width, height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
// Must implement the pure virtual function getArea()
double getArea() const override {
return width * height;
}
};
// You CANNOT create an object of Shape directly:
// Shape myShape; // Error! Cannot instantiate abstract class
// You CAN create objects of derived concrete classes:
// Circle myCircle(5);
// Rectangle myRectangle(4, 6);
Implications for Derived Classes
As the reference states, classes derived from an abstract class must implement the pure virtual function or they, too, are abstract classes.
- If a derived class provides implementations for all inherited pure virtual functions, it becomes a concrete class, and you can create objects of that class.
- If a derived class fails to implement one or more inherited pure virtual functions, it automatically becomes an abstract class itself, and you cannot create objects of that derived class either. This pattern can continue down an inheritance hierarchy.
Summary Table: Abstract vs. Concrete Classes
Feature | Abstract Class | Concrete Class |
---|---|---|
Instantiation | Cannot create objects | Can create objects |
Pure Virtual Functions | Contains at least one (= 0 ) |
Contains none |
Purpose | Defines interface, serves as base | Provides full implementation, usable |
Derived Classes Need | Must implement pure virtuals (or be abstract) | N/A (unless they are bases themselves) |
In conclusion, creating an abstract class is a fundamental concept in object-oriented programming, used to enforce a structure and contract upon derived classes.