Inheritance lets a class (the derived/child class) reuse and extend the members of another class (the base/parent class).
class Vehicle {
protected: // accessible in Vehicle AND its derived classes
int speed = 0;
public:
void accelerate(int amount) { speed += amount; }
};
class Car : public Vehicle { // Car inherits everything from Vehicle
public:
void honk() { std::cout << "Beep!\n"; }
};
int main() {
Car myCar;
myCar.accelerate(50); // inherited from Vehicle
myCar.honk(); // Car's own method
return 0;
}
protected sits between private and public: inaccessible outside the class hierarchy entirely, but reachable from derived classes — exactly the access level speed needs here, since Car should be able to use it but outside code shouldn't touch it directly.
The public/protected/private keyword before the base class name (class Car : public Vehicle) controls how the base class's access levels carry over into the derived class — public inheritance (by far the most common) preserves them exactly as they were, which is what "is-a" relationships (a Car is a Vehicle) require.
C++ additionally allows multiple inheritance — a class inheriting from more than one base class simultaneously — which many other object-oriented languages (Java, C#) deliberately don't allow, precisely because it introduces the diamond problem: if two base classes both inherit from a common ancestor, a derived class inheriting from both ends up with two separate copies of that ancestor's data, an ambiguity resolved with virtual inheritance when it's genuinely needed — a relatively advanced, less commonly used feature best introduced once single inheritance is comfortable.