25.7 — Pure virtual functions, abstract base classes, and interface classes

Pure virtual (abstract) functions and abstract base classes So far, all of the virtual functions we have written have a body (a definition). However, C++ allows you to create a special kind of virtual function called a pure virtual function (or abstract function) that has no body at all! A …

25.6 — The virtual table

Consider the following program: #include <iostream> #include <string_view> class Base { public: std::string_view getName() const { return “Base”; } // not virtual virtual std::string_view getNameVirtual() const { return “Base”; } // virtual }; class Derived: public Base { public: std::string_view getName() const { return “Derived”; } virtual std::string_view getNameVirtual() const …

25.4 — Virtual destructors, virtual assignment, and overriding virtualization

Virtual destructors Although C++ provides a default destructor for your classes if you do not provide one yourself, it is sometimes the case that you will want to provide your own destructor (particularly if the class needs to deallocate memory). You should always make your destructors virtual if you’re dealing …

25.1 — Pointers and references to the base class of derived objects

In the previous chapter, you learned all about how to use inheritance to derive new classes from existing classes. In this chapter, we are going to focus on one of the most important and powerful aspects of inheritance — virtual functions. But before we discuss what virtual functions are, let’s …

25.8 — Virtual base classes

Last chapter, in lesson , we left off talking about the “diamond problem”. In this section, we will resume this discussion. Note: This section is an advanced topic and can be skipped or skimmed if desired. The diamond problem Here is our example from the previous lesson (with some constructors) …

24.9 — Multiple inheritance

So far, all of the examples of inheritance we’ve presented have been single inheritance — that is, each inherited class has one and only one parent. However, C++ provides the ability to do multiple inheritance. Multiple inheritance enables a derived class to inherit members from more than one parent. Let’s …