Inheritance and Polymorphism Summary

What Is Inheritance?

  • Inheritance is a key feature of Object-Oriented Programming (OOP).
  • It's a form of software reuse that enhances code readability.
  • Allows a new class (derived class) to be based on an existing class (base class), inheriting member variables and functions (except constructors and destructor).

Key Concepts

  • Base Class (Parent): The general class.
  • Derived Class (Child): The specialized class.
  • "Is-a" Relationship: Indicates that one object is a specialized version of another.
  • Specialization: Creating specific classes by inheriting from a parent class.
  • Generalization: Creating a general class as a base for more specific classes.

How Inheritance Works

  • The derived class inherits members (variables and functions) without rewriting.
  • New members can be added to the derived class.
  • Inheritance is achieved using the : operator.

Code Example (Without Inheritance)

  • Demonstrates Person and Student classes without inheritance, showing redundant code.

Code Example (With Inheritance)

  • Demonstrates inheritance where Student class inherits from Person class.
  • Common attributes/functions are in the base class (Person).

Accessing Inherited Members

  • Public members of the Person class become public members of the Student class.
  • Private members of the Person class are inherited but inaccessible directly by the Student class; accessed indirectly through base class member functions.

Protected Access Specification

  • C++ provides protected access specification.
  • Protected members of a base class are like private members but accessible by functions in a derived class.

Modes of Inheritance

  • Base class access specification (public, protected, private) affects how inherited members are accessed in the derived class.
  • Default access specification is private if left out.

Constructor and Destructor Inheritance

  • Base class constructors and destructors are not inherited.
  • Base class constructor is called before the derived class constructor.
  • Destructors are called in reverse order.
  • If the base class has only parameterized constructors, the derived class must explicitly call the base class constructor in its own constructor.

Class Hierarchies

  • A base class can be derived from another class, creating a hierarchy.
  • Class C inherits Class B’s members, including those Class B inherited from Class A.

Polymorphism

  • Polymorphism means "having multiple forms".
  • Types:
    • Compile-time (static) polymorphism:
    • Method overloading (same name, different parameters).
    • Operator overloading (defining operator behavior for user-defined types).
    • Run-time (dynamic) polymorphism:
    • Method overriding (child class redefining a parent class method).

Compile-Time Polymorphism

  • Function Overloading: Multiple functions with the same name but different parameter types or counts.
  • Operator Overloading: Defining how operators work for user-defined types.

Run-Time Polymorphism

  • Function overriding occurs when a derived class provides its own implementation for a method defined in its base class. The function to be called is determined at runtime.

Overloading vs. Overriding

  • Overloading: Provides multiple versions of a function in the same class; uses compile-time polymorphism; must have different parameter lists.
  • Overriding: Provides a new implementation for an inherited function in a derived class; uses run-time polymorphism; must have the same signature.