C++ class inheritance, key concept

What’s inherited

All base class members are inherited to derived class, except for:

  1. its constructors and its destructor
  2. its assignment operator members (operator=)
  3. its friends

Note: private members are inherited but not directly accessible by the derived class.

Inheritance access specifier

  1. public: mostly used. Allows the derived class to access public and protected members of the base class.
  2. protected: turns public members of the base class to potected.
  3. private: turns public and protected members of the base class to private.

Order of execution

inherit access

Code Example

Below is an example illustrating these relationships:


Explanation

Class Parent:

Class Child1:

Class Child2:

Main Function:

Conclusion

Understanding access specifiers and inheritance is crucial for designing robust and maintainable object-oriented programs in C++. Constructors and destructors help manage the lifecycle of objects, ensuring proper initialization and cleanup.