Understanding Inheritance in C++: A Beginner's Practice Guide with Hands-On Examples

Introduction

Inheritance is one of the four fundamental principles of Object-Oriented Programming (OOP). It is a mechanism through which a new class, known as the derived class or child class, acquires the properties and behaviors of an existing class, known as the base class or parent class.

Inheritance promotes code reusability by allowing common attributes and methods to be defined once in the base class and reused by multiple derived classes. A derived class can access the inherited members of its parent class while also introducing its own unique data members and member functions. This helps reduce code duplication and makes programs easier to maintain and extend.

This document presents two practical examples demonstrating the implementation of inheritance in C++.

Example 1: The Employee Hierarchy

This example demonstrates how a derived class inherits data members and member functions from a base class while extending its functionality with additional attributes and methods.

Program


Explanation

The program defines a base class named Employee, which contains two public data members: name and baseSalary. It also provides the displayBasicDetails() member function to display the employee's basic information.

The Manager class publicly inherits from the Employee class using the public inheritance specifier. Because of inheritance, the Manager class automatically gains access to the inherited data members (name and baseSalary) and the inherited member function (displayBasicDetails()).

In addition to the inherited members, the Manager class introduces its own data member named annualBonus and defines a new member function called displayTotalCompensation(). This function first displays the inherited employee details and then calculates and displays the manager's total compensation by adding the annual bonus to the base salary.

Inside the main() function, an object of the Manager class is created. The inherited members and the derived class member are assigned values, and the displayTotalCompensation() function is called to display all the information. This example demonstrates how inheritance allows derived classes to reuse existing functionality while extending it with additional features.

Example 2: The Media System

This example demonstrates inheritance by creating a specialized Book class from a general MediaItem class. The derived class reuses the common information provided by the base class while adding attributes specific to books.

Program


Explanation

The MediaItem class serves as the base class and stores common information shared by different types of media, including the title and publisher. It also provides the printCoreInfo() member function to display this information.

The Book class publicly inherits from MediaItem, automatically acquiring all of its public data members and member functions. In addition to the inherited members, the Book class introduces a new data member named pageCount, which stores the number of pages in the book.

The printBookSummary() member function first calls the inherited printCoreInfo() function to display the common media information and then displays additional details specific to books.

In the main() function, an object of the Book class is created. The inherited members (title and publisher) and the derived class member (pageCount) are initialized with appropriate values. Finally, the printBookSummary() function is called to display the complete book information. This example illustrates how inheritance enables specialized classes to reuse common functionality while introducing their own unique characteristics.

Conclusion

Inheritance is a powerful feature of Object-Oriented Programming that enables one class to acquire the properties and behaviors of another class. It promotes code reusability, simplifies program development, and establishes logical relationships between classes. By allowing derived classes to extend existing functionality instead of rewriting common code, inheritance makes software more organized, maintainable, and scalable. As demonstrated in the Employee Hierarchy and Media System examples, derived classes can inherit attributes and methods from their parent classes while adding their own specialized features to meet specific requirements.