While all the above solutions are correct, I'd like to add a real world example. Consider a drawing program that lets you draw line graphics using various shapes, such as lines, squares, or circles. As you might be aware of, everytime a window size changes, or when hidden parts of the window get exposed, you have to redraw your image containing these shapes. So you develop a class called 'Scene' that manages the drawing and redrawing:
class Scene {
private:
std::list<class Shape*> shapes_;
public:
void redraw();
};
This class contains a list of shapes that it has to redraw. However, you are just starting your program, and you don't know yet what kind of shapes you'd want to support, and the few you already thought of are stored and drawn in different ways. So you make 'Shape' a base class, and derive 'Line', 'Square' and 'Circle' from that base. The code to draw these shapes should also be implemented in each shape seperately, so you add a virtual function 'draw' to Shape, that gets overridden by the concrete, derived shape classes:
class Shape {
public:
virtual ~Shape();
virtual void draw() = 0;
};
class Line : public Shape {
private:
Point p1_;
Point p2_;
public:
virtual ~Line();
void draw();
};
class Square : public Shape {
private:
Point position_;
double width_;
public:
virtual ~Square();
void draw();
};
class Circle : public Shape {
private:
Point position_;
double radius_;
public:
virtual ~Circle();
void draw();
};
It makes little sense to implement
draw()
in the class
Shape
, since Shape itself does not define any elements that can be drawn. Therefore I declared
draw()
a
pure virtual function. This, by definition, turns
Shape
into an
abstract class. As a result, you will not be able to create an instance of type 'Shape'.
Objects of type Shape simply don't exist, they exist only in the form of derived classes,
Line
,
Circle
, or
Square
. These classes are considered
concrete, since instances of these types actually exist. Shape OTOH is considered
abstract, because no objects of this specific type exists, and the class is only used to describe a concept or common property of a certain range of object types.