WHAT IS INHERITANCE?
It's an OOP pillar that lets classes inherit properties and behaviors from parent classes. Think of it as a family tree for your code, where children automatically get traits from their parents.
The Call of Duty weapon system shows inheritance architecture:
Save the complete code: https://lnkd.in/g953iK_Z
1. Weapon (Base Class): Contains universal properties (damage, range) and defines the interface with virtual methods that ALL weapons must implement. Pure virtual Attack() ensures every weapon knows how to attack.
2. Category Classes (Firearm, Launcher, Melee): Inherit from Weapon but add category-specific behavior. Firearms have ammo systems, Launchers handle explosions, Melee weapons override Attack() with swing mechanics.
3. Specific Weapons (AssaultRifle, RocketLauncher): These leaf classes inherit their category's behavior but mainly differ in configuration data.
WHEN TO USE INHERITANCE?
When you have a clear "IS-A" relationship:
- An AssaultRifle IS-A Firearm
- A Firearm IS-A Weapon
This structure allows designers to easily add new weapons without engineering help - they just configure data for a new assault rifle rather than coding entirely new classes!
The polymorphism benefit is HUGE: the game can treat ALL weapons as the base Weapon type while still getting specialized behavior through virtual function overrides.