BIT153 ยท 2082 ยท 5 marks
What is the difference between abstract class and concrete class? Explain with example.
What is the difference between abstract class and concrete class? Explain with example. [5]
Quick answer
C++ has no abstract keyword.
Abstract Class and Concrete Class in C++
What makes a class abstract in C++
C++ has no abstract keyword. A class becomes abstract the moment it declares at least one pure virtual function, written by putting = 0 after a virtual member function instead of a body:
virtual double area() const = 0; // pure virtual, so the class is abstract
An abstract class cannot be instantiated. The compiler refuses Shape s; with "cannot declare variable of abstract type". It can still be used as a base type: pointers and references of the abstract type are perfectly legal, and they are exactly how runtime polymorphism is obtained.
Because objects are usually deleted through a base pointer, an abstract base class should also declare a virtual destructor, otherwise the derived part of the object is never destroyed.
Key characteristics:
- At least one member function is declared pure virtual with
= 0. - No object of the class can be created, though base pointers and references are allowed.
- It may also contain ordinary data members and fully implemented member functions.
- A derived class stays abstract until it overrides every pure virtual it inherits.
What a concrete class is
A concrete class supplies a body for every function it declares or inherits, so nothing is left unimplemented. It can be instantiated directly, and it is the only kind of class from which real objects are made. A concrete class is very often a class derived from an abstract base that has filled in the missing implementations.
Key characteristics:
- Every inherited pure virtual function has been overridden.
- Objects can be created on the stack, with
new, or inside a container. - It may be a standalone class or the leaf of an inheritance hierarchy.
Comparison
| Feature | Abstract class | Concrete class |
|---|---|---|
| Instantiation | Not possible, only pointers and references | Objects can be created directly |
| Functions | Has at least one pure virtual (= 0) | Every function has a body |
| How it is declared | No keyword, the = 0 on a virtual marks it | Nothing special is written |
| Purpose | Defines an interface or a common template | Provides a usable, complete type |
| Destructor | Should be declared virtual | Virtual only if it is itself a base |
In Java the same idea is written with the abstract keyword on the class and on the method; in C++ the = 0 on a virtual function carries that meaning by itself.
Worked example
#include <iostream>
#include <string>
// Abstract class: it declares a pure virtual function, so no Shape object can exist.
class Shape {
protected:
std::string color;
public:
explicit Shape(const std::string& c) : color(c) {}
virtual ~Shape() = default; // virtual destructor for safe deletion via a base pointer
virtual double area() const = 0; // pure virtual: this is what makes Shape abstract
void displayColor() const { // an ordinary member, already implemented
std::cout << "Color: " << color << "\n";
}
};
// Concrete class: it gives a body to the inherited pure virtual function.
class Circle : public Shape {
double radius;
public:
Circle(double r, const std::string& c) : Shape(c), radius(r) {}
double area() const override { return 3.14159 * radius * radius; }
};
// Another concrete class from the same abstract base.
class Rectangle : public Shape {
double length, width;
public:
Rectangle(double l, double w, const std::string& c)
: Shape(c), length(l), width(w) {}
double area() const override { return length * width; }
};
int main() {
// Shape s("Red"); // compile error: cannot declare variable of abstract type Shape
Circle c(5.0, "Red");
c.displayColor();
std::cout << "Circle area: " << c.area() << "\n";
Rectangle r(4.0, 6.0, "Blue");
r.displayColor();
std::cout << "Rectangle area: " << r.area() << "\n";
const Shape& s = c; // a reference to the abstract type is legal and dispatches virtually
std::cout << "Through base reference: " << s.area() << "\n";
return 0;
}
Output:
Color: Red
Circle area: 78.5398
Color: Blue
Rectangle area: 24
Through base reference: 78.5398
Here Shape is the abstract class. It fixes the interface every shape must offer, area(), without deciding how any particular shape computes it, and it supplies one ready made service, displayColor(). Circle and Rectangle are the concrete classes: each overrides area(), so each can be instantiated. The commented out line shows the practical difference, since only the concrete types produce objects while the abstract type produces the common contract they all obey.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.