# C++ Inheritance Basic ## What's Inheritance Inheritance lets you create a **new class (child / derived class)** from an **existing class (parent / base class)**. The new class gets: - All public and protected attributes and methods of the parent - The ability to override or extend behaviors ## Basic syntax ```c++ class BaseClass { // base class members }; class DerivedClass : access-specifier BaseClass { // derived class members }; ``` ## Access specifiers C++03 offers three types of inheritance: 1. Public Inheritance (most common) ```c++ class Derived : public Base { /*...*/ }; ``` - Public members of Base → Public in Derived - Protected members of Base → Protected in Derived - Private members of Base → Not accessible in Derived 2. Protected Inheritance ```c++ class Derived : protected Base { /*...*/ }; ``` - Public and protected members of Base → Protected in Derived - Private members of Base → Not accessible in Derived 3. Private Inheritance ```c++ class Derived : private Base { /*...*/ }; ``` - All accessible members of Base → Private in Derived ## Key points ### Constructor/Destructor Order - Base class constructor is called first, then derived - Destructors are called in reverse order - Why reverse order? - Dependency management - Derived classes depend on their Base classes - Member objects depend on the containing object being valid - Can't safely destroy the base class while the derived class still stands - Resource cleanup safety - Derived classes may use base class resources - Object consistency - During destruction, the object is still technically "alive" until its destructor completes ### Function Overriding - Derived classes can override base class methods - Override vs Overload - Override: redefine a parent function in the child class - Only works if the function is virtual in the base class - Overload: having functions with the same name but different parameters - Example - ScavTrap/ FragTrap can change the behavior of ```attack()``` without ```ClapTrap::attack()``` being virtual - But won't get **polymorphism **when using base class pointers or reference unless it's virtual ### Polymorphism in C++ - Polymorphism means “many forms”. - In C++, it allows objects of different classes (that are related by inheritance) to be treated as objects of a common base class, but behave differently. - Example of run-time polymorphism - Implementation ```c++ class ClapTrap { public: virtual void attack(const std::string& target) { std::cout << "ClapTrap attacks " << target << " 💥\n"; } }; class ScavTrap : public ClapTrap { public: void attack(const std::string& target) override { std::cout << "ScavTrap attacks " << target << " with gates! 🔥\n"; } }; ``` - Main function ```c++ ClapTrap* bot = new ScavTrap("Scavy"); bot->attack("enemy"); ``` - Output ```c++ ScavTrap attacks enemy with gates! 🔥 ``` - How does it works - Declare the base class method as virtual - Override it in the derived class - Use a base class poiner / reference - The compiler uses a vtable at run time to decide which version to call - When to use polymorphism - When you want to treat all derived classes uniformly, but allow them to have custom behavior - eg. a function that accepts a ```ClapTrap&``` and works with ```ScavTrap, FragTrap, DiamondTrap```, etc.