C++ zyBooks Chapter 5 - Inheritance

ECE 2620, Fall 2019


Table of Contents


1. Derived Classes

z5.1-derived.cc - class ProduceItem is derived from class GenericItem

The term derived class (or subclass) refers to a class that is derived from another class that is known as a base class (or superclass).

Any class may serve as a base class; no changes to the definition of that class are required.

The derived class is said to inherit the properties of its base class, a concept commonly called inheritance.

An object declared of a derived class type has access to all the public members of the derived class as well as the public members of the base class.


2. Multiple Derivations


3. Access Specifiers

The members of a derived class have access to the public and protected members of the base class, but not to the private members of the base class.


4. Derived Access


5. Overriding Member Functions

z5.3-override.cc - ProduceItem's PrintItem() function overrides GenericItem's PrintItem() function.
class GenericItem {
public:
...
   void PrintItem() {
      cout << itemName << " " << itemQuantity << endl;
   };

protected:
   string itemName;
   int itemQuantity;
};

class ProduceItem : public GenericItem { // Derived from GenericItem
public:
...
   void PrintItem() {
      cout << itemName << " " << itemQuantity
      << " (Expires: " << expirationDate << ")"
      << endl;
   };

private:
   string expirationDate;
};

6. Overriding vs. Overloading


7. Virtual Member Functions

Polymorphism refers to determining which program behavior to execute depending on data types.

Function overloading is a form of compile-time polymorphism wherein the compiler determines which of several identically-named functions to call based on the function's arguments.

Another form is runtime polymorphism wherein the compiler cannot make the determination but instead the determination is made while the program is running.

When the object is a pointer, the indication of virtual causes the program to dynamically determine the correct function to call based on the pointer type.

z5.4-virtual.cc - runtime polymorphism via a virtual function - compare with and without virtual

  Smith Cereal 9
  Apple 40 (Expires: May 5, 2012)

  Inventory:
  Smith Cereal 9
  Apple 40 (Expires: May 5, 2012)
  Smith Cereal 9
  Apple 40 (Expires: May 5, 2012)

  Inventory:
  Smith Cereal 9
  Apple 40

8. Abstract Class & Pure Virtual Function

Virtual usually means something doesn't really exist, whereas a virtual function in a base class does exist; the word virtual just relates to how that function gets overridden.

In contrast, one can create a truly virtual function, known as a pure virtual function, using the following syntax:

  class GenericItem {
        ...
        virtual void PrintItem() = 0;
        ...
  };
A class that has at least one pure virtual function is known as an abstract base class, meaning objects cannot be declared of that class.

The function must be defined and implemented in a derived class.