C++ interface demonstrator

Code documentation of the simply C++ interface demonstrator. You can find the corresponding codes under the applications/preliminaries/cpp-interface.

class VShape2D

Interface, i.e. base class with a pure virtual method, for 2D shape area computations.

Author

M. Novak

Date

May 2022

This base class is an interface for 2D shape area computations. It has its pure virtual Area() method. Therefore, this method must be implemented by each derived classes. This ensures, that whatever type of an actuall 2D shape is (derived from this base class) the Area() interface method will be available. Therefore, the rest of the code can be developed without knowing what 2D shapes will be eventually there: they will be all VShape2D types providing their own implementation of the Area() interface method defined in this base class.

Note, that the pure virtual method makes this base class abstract, i.e. objects cannot be instantiated directly form this base class.

Subclassed by Circle, Square

Public Functions

virtual double Area() = 0

The area computation interface method.

Example of a pure virtual method that makes this base class abstract.

Each 2D shape has an area but all computed differently depending on the actual type of the shape. This is why it’s a pure virtual method, i.e. each derived class must implement.

inline virtual double Perimeter()

Optional perimeter computation method.

Example of a virtual method with defult implementation in the base class.

Each 2D shape has its own way of computing the perimeter just like the area. However, we decided that actually the perimeter is not important for our algorithm in most cases of shapes. Therefore, this default implementation is available for each derived class and will be invoked unless the concrete derived class provides its own implementation. The derived Square class implements this method while Circle relyes on this defult base class implementation.

class Square : public VShape2D

Derived class that implements the VShape2D interface for Square-s.

Author

M. Novak

Date

May 2022

Public Functions

inline virtual double Area() override

Actual implementation of the area computation interface method of the base class.

Square must implement the VShape2D::Area base class method since that’s a pure virtual method.

Note

The override keyword is very useful: indicates that this method implements a virtual method of the base class so the compiler is aware of that intention.

inline virtual double Perimeter() override

The optional Perimeter interface method implementation.

class Circle : public VShape2D

Derived class that implements the VShape2D interface for Circle-s.

Author

M. Novak

Date

May 2022

Public Functions

inline virtual double Area() override

Actual implementation of the area computation interface method of the base class.

Circle must implement the VShape2D::Area base class method since that’s a pure virtual method.