cpp

Chapter 16: Inheritance

Overview of Inheritance

  • Inheritance is a fundamental concept in Object-Oriented Programming (OOP), allowing for hierarchical classifications.

  • General (base) classes can be extended by more specific (derived) classes.

  • A derived class can even become a base class for another derived class, facilitating multiple inheritance.

Class Hierarchy Terminology

  • Base Class: The class being inherited from.

  • Derived Class: The class that inherits from the base class.

Base-Class Access Control

  • The syntax for inheritance is structured as follows:

    class derived-class-name: access-specifier base-class-name {
        // body of derived class
    };
  • Access specifiers:

    • Public: Public base members remain public in the derived class; protected members remain protected, and private members stay private.

    • Private: All public and protected base members become private in the derived class.

    • Protected: Members of the base class remain protected in the derived class.

Example:
#include <iostream>
using namespace std;
class base {
    int i, j;
public:
    void set(int a, int b) { i=a; j=b; }
    void show() { cout << i << " " << j << "\n"; }
};
class derived : public base {
    int k;
public:
    derived (int x) { k=x; }
    void showk() { cout << k << "\n"; }
};

int main() {
    derived ob(3);
    ob.set(1, 2);
    ob.show();
    ob.showk();
    return 0;
}

Protected Members and Inheritance

  • The protected access specifier allows members to be accessible in the derived class but not by the outside code.

  • Example of behavior:

class base { protected: int i, j; public: void set(int a, int b) { i=a; j=b; } void show() { cout << i << " " << j << "
"; } }; class derived : public base { int k; public: void setk() { k = i * j; } void showk() { cout << k << "
"; } };


### Multiple Inheritance
- A derived class can inherit from multiple base classes using a comma-separated format:
```cpp
class derived: public basel, public base2 {
 // body
};

Constructors and Destructors in Inheritance

  • Construction Order: Base class constructor is called first, followed by the derived class constructor.

  • Destruction Order: Derived class destructor is called first, followed by the base class destructor.

Example:
class base {
public:
    base() { cout << "Constructing base\n"; }
    ~base() { cout << "Destructing base\n"; }
};
class derived : public base {
public:
    derived() { cout << "Constructing derived\n"; }
    ~derived() { cout << "Destructing derived\n"; }
};

int main() {
    derived ob;
    return 0;
}

Passing Parameters to Base-Class Constructors

  • To pass parameters to base class constructors, utilize an expanded syntax in derived class constructor declaration:

derived(int arg1, int arg2): base(arg2) { // body }


### Granting Access to Members
- When base class members are inherited as private, access can be restored using the **using** statement or access declarations.
```cpp
class derived: private base {
public:
 using base::member_name;
};

Virtual Base Classes

  • Virtual inheritance prevents multiple copies of the base class in instances of derived classes.

  • Usage:

class derived1 : virtual public base {} class derived2 : virtual public base {}

- This ensures only one instance of the base class exists, resolving ambiguity in member access.