Tutorial by Examples

The typical example is an abstract shape class, that can then be derived into squares, circles, and other concrete shapes. The parent class: Let's start with the polymorphic class: class Shape { public: virtual ~Shape() = default; virtual double get_surface() const = 0; virtual vo...
Suppose that you have a pointer to an object of a polymorphic class: Shape *ps; // see example on defining a polymorphic class ps = get_a_new_random_shape(); // if you don't have such a function yet, you // could just write ps = new Square...
If a class is intended to be used polymorphically, with derived instances being stored as base pointers/references, its base class' destructor should be either virtual or protected. In the former case, this will cause object destruction to check the vtable, automatically calling the correct destruc...

Page 1 of 1