TeachMate
My ProgressJoin Session

Inheritance

0/2 completed · ~0h 5m

All Topics
OOPLearnInheritance
1 / 2

Inheritance Basics

beginner
2 min read

Inheritance allows a new class to inherit properties and behaviors from an existing class.

Base Class (Parent)

The class being inherited from.

Derived Class (Child)

The class that inherits.

Syntax

class Animal {
public:
    void eat() { cout << "Eating"; }
};

class Dog : public Animal {
public:
    void bark() { cout << "Barking"; }
};

Dog inherits eat() from Animal and adds bark().

Types of Inheritance

  • public: Most common. Public stays public, protected stays protected
  • private: Everything becomes private in derived class
  • protected: Public becomes protected in derived class

Benefits

  • Code reuse: Write common code once in base class
  • Logical hierarchy: Model real-world relationships
  • Polymorphism: Treat derived classes through base class interface

Example Hierarchy

Animal -> Dog, Cat, Bird

Vehicle -> Car, Truck, Motorcycle

Key takeaway: Inheritance enables code reuse and logical hierarchies — prefer public inheritance and compose when possible.

TeachMate - Interactive learning platform for bachelor students

HomeLearnJoin Session|2 Subjects