Inheritance Types and Member Functionality in C++

Different Types of Inheritance

  • Inheritance is a fundamental concept in object-oriented programming that allows a class to inherit properties and behavior (methods) from another class. Here are the different types of inheritance:

Single Inheritance

  • Definition: A type of inheritance where a single derived class inherits from a single base class.

Multilevel Inheritance

  • Definition: A form of inheritance where a class derives from another class, which in turn derives from another class, forming a multi-layer hierarchy.

Multiple Inheritance

  • Definition: A type of inheritance where a class can inherit from more than one base class, enabling it to acquire features from multiple sources.

Hierarchical Inheritance

  • Definition: Inheritance that occurs when multiple derived classes inherit from a single base class, forming a hierarchy.

Hybrid Inheritance

  • Definition: A combination of two or more types of inheritance systems used in various ways, often a combination of single and multiple inheritance.


Example of Inheritance

  • Code Snippet 1 (Page 2 & Page 3):

  class A { protected: int num1 = 1; };
  class B { protected: int num2 = 2; };
  class C: public A, public B {
      protected: int num3 = 3;
      public: void print() {
          cout << num1 << num2 << num3;
      }
  };
  int main() {
      C c;
      c.print();
      return 0;
  }
  • Output: The expected output of this snippet is 123.


Constructors and Destructors

  • Code Snippet 2 (Page 4):

  class A {
      protected: int num1 = 1;
      public: A() {
          cout << "A constructor called\n";
      }
      ~A() {
          cout << "A destructor called\n";
      }
  };
  • Important Note: A constructor is called when an object is created, and a destructor is invoked when an object is destroyed.


Detailed Example with Constructor Calls

  • Code Snippet 3 (Page 6-7):

  class A {
      protected: int num1;
      public: A(int n): num1(n) {
          cout << "A constructor called\n";
      }
      ~A() {
          cout << "A destructor called\n";
      }
  };

  class B {
      protected: int num2;
      public: B(int n): num2(n) {
          cout << "B constructor called\n";
      }
      ~B() {
          cout << "B destructor called\n";
      }
      void print() {
          cout << num2 << endl;
      }
  };

  class C: public B, public A {
      protected: int num3 = 3;
      public: C(int num1, int num2, int num3): A(num1), B(num2) {
          cout << "C constructor called\n";
      }
      ~C() {
          cout << "C destructor called\n";
      }
      void print() {
          cout << num1 << " " << num2 << " " << num3 << " ";
      }
  };

  int main() {
      C c(1, 2, 3);
      c.print();
      return 0;
  }
  • Output: Constructor calls will show in the order A constructor called, B constructor called, C constructor called, followed by destructor calls during the destruction of C, B, and A respectively.


Ambiguities in Multiple Inheritance

  • Example from Page 11:

  // This will raise an error due to ambiguity.
  void print() {
      cout << num1 << num1 << num3 << num4;
  }
  • Solution: Specifying the class scope (e.g., A::num1 and B::num1) can help clarify which member variable to use when there's ambiguity due to multiple inheritance.


Virtual Inheritance

  • Definition: Virtual inheritance helps solve the ambiguity in multiple inheritance where a derived class can inherit from classes that have a common base class. The virtual base class is constructed before any other derived classes in multiple inheritance.

  • Example from Pages 20-22 demonstrates virtual base class behavior and constructor order.

  • Key Takeaway: The virtual base class constructor is called before any derived class constructors to ensure a single instance is used in derived classes.


Functionality in Derived Classes

  • Example Subclassing for Print Function (Page 24):

  void print() {
      cout << B::num1 << " " << C::num1 << " " << E::num1 << endl;
  }
  • Explanation: This approach ensures clarity when referencing member variables from multiple bases.


Summary of Constructor and Destructor Calls

  • Constructor Call Order: The constructors are called in the order of inheritance starting from the base classes downwards. Likewise, destructors are called in the reverse order after exiting the program or releasing resources.

  • Destruction Sequence: Destructor calls start from the most derived class back to the base class, ensuring a proper cleanup of resources and memory.