Inheritance/Polymorphism  «Prev  Next»
Lesson 2 What is inheritance?
Objective Define inheritance and describe its advantages.

Define Class Inheritance in C++

Inheritance is a powerful code reuse mechanism. Through inheritance, you can derive a new class from an already-existing class. In other words, the existing class can be added to or altered to create the derived class. This creates a hierarchy of related types that share code and interface. Many useful types are variants of one another, and it is frequently tedious to produce the same code for each. A derived class inherits the description of the base class. The derived class can then be altered by adding members, modifying existing member functions, and modifying access privileges. Inheritance allows a new class to be defined by extending or modifying one or more existing classes. The new class is called a derived class and the existing class is its base class.
A derived class can itself serve as a base class for other derived classes, enabling us to build hierarchies of classes related by inheritance. Inheritance allows us to avoid reinventing the wheel: instead of having to define every class from scratch, we can use existing classes as foundations on which to build new classes.
Inheritance is a fundamental concept in C++, integral to its object-oriented programming paradigm. It refers to the ability to define a new class based on an existing class, thereby promoting code reuse and modularity.
The existing class, known as the "base" or "parent" class, provides attributes (data members) and behaviors (member functions or methods) that are inherited by the new "derived" or "child" class. The derived class may then add or override these attributes and behaviors to customize or extend the functionality provided by the base class.
The syntax for defining a derived class is as follows:
class DerivedClass: visibility-mode BaseClass

The visibility-mode can be public, protected, or private, and it determines how the members of the base class are accessed in the derived class. There are several key advantages of using inheritance in C++:
  1. Code Reusability: Inheritance allows the derived class to reuse the code written in the base class, thereby reducing code redundancy and maintaining a DRY (Don't Repeat Yourself) codebase.
  2. Extensibility: Inheritance provides a convenient way to extend the functionality of existing classes without altering their source code.
  3. Data Hiding: The encapsulation properties of classes are preserved in inheritance. Base class details can be hidden from the derived class by making them private.
  4. Overriding and Polymorphism: A derived class can override methods of the base class, facilitating polymorphism, which enhances code flexibility and readability.
  5. Hierarchical Classification: Inheritance helps in the categorization of classes through a hierarchy that represents is-a relationships (e.g., a "Dog" is-a "Mammal"), aligning with real-world models.

Understanding and utilizing inheritance effectively can greatly improve the structure, maintainability, and scalability of C++ code.

Code Reuse

One of the most compelling features about C++ is code reuse. But to be revolutionary, you need to be able to do a lot more than copy code and change it. That is the C approach, and it has not worked very well. As with most everything in C++, the solution revolves around the class. You reuse code by creating new classes, but instead of creating them from scratch, you use existing classes that someone else has built and debugged. The trick is to use the classes without soiling the existing code. In this chapter you will see two ways to accomplish this. The first is quite straightforward: You simply create objects of your existing class inside the new class. This is called composition because the new class is composed of objects of existing classes. The second approach is subtler. You create a new class as a type of an existing class. You literally take the form of the existing class and add code to it, without modifying the existing class. This magical act is called inheritance, and most of the work is done by the compiler. Inheritance is one of the cornerstones of object-oriented programming and has additional implications.
It turns out that much of the syntax and behavior are similar for both composition and inheritance (which makes sense; they are both ways of making new types from existing types).