Inheritance/Polymorphism  «Prev  Next»
Lesson 18

Inheritance and Pure polymorphism Conclusion

Public Inherit
This module explored the fundamentals of inheritance and pure polymorphism, including the use of derived classes and virtual functions to create a class hierarchy.
You learned:
  1. What inheritance is and why it is so important to the object-oriented programming paradigm
  2. What virtual member functions are and how they are used
  3. How to derive a class from a base class and create a class hierarchy
  4. How public inheritance implements an ISA relationship between a derived class and the base class
  5. How a reference to the derived class may be implicitly converted to a reference to the public base class
  6. What pure virtual functions are and why they are useful in creating abstract base classes
  7. The uses of the C++-specific cast operators
  8. How to use Runtime Type Identification (RTTI) to safely determine the type pointed at by a base class pointer at runtime
  9. How to code exceptions to catch unexpected conditions

Inheritance Defined

Inheritance is a mechanism for enhancing existing, working classes. If a new class needs to be implemented and a class representing a more general concept is already available, then the new class can inherit from the existing class. For example, suppose we need to define a class Manager. We already have a class Employee, and a manager is a special case of an employee. In this case, it makes sense to use the language construct of inheritance. Here is the syntax for the class definition:
class Manager : public Employee{
 public:
  new member functions
 private:
  new data members
};

The : symbol denotes inheritance. The keyword public is required for a technical reason.
The existing, more general class is called the base class. The more specialized class that inherits from the base class is called the derived class. In our example, Employee is the base class and Manager is the derived class.
In the Manager class definition you specify only new member functions and data members. All member functions and data members of the Employee class are automatically inherited by the Manager class. For example, the set_salary function automatically applies to managers:

Manager m;
m.set_salary(68000);