Mastering C++: Part 3: Abstract Classes

Learning C++

Using C++

Mastering C++


Abstract Classes

The object-oriented programming model is based on two relationships: is-a and has-a. Objects can have things (properties and functions) and also be things.

In the case of using objects that are based (inherit from) other objects, these are the other objects. Their relationship is is-a.

In the case of a Programmer being a kind of Person, a programmer is-a kind of person.

Virtual

To help with building objects, there exists a keyword called virtual that defines an “empty” function that other objects define.

Abstract

Objects that have at least one virtual function are called abstract. These usually serve as the basis for others and define the shape of later objects.

Types of Virtual

There are two types of virtual functions: regular and pure. A virtual function is a pure virtual function when it is defined as set to zero.

A regular virtual function can be defined by the object that inherit it. In order to use the function, it must define it within itself. However, a pure virtual function must define it or become itself an abstract class.

Play with the example on Repl.it!