The short answer
Inheritance lets a class build on another class. A derived class gets the members of its base class and can add to them or change them. Polymorphism lets code use different types through one interface, with each object choosing its own behavior.
The two often work together, but neither needs the other. Inheritance on its own reuses code. Polymorphism is about how a call picks its behavior, and that can come from virtual functions or from templates.
What inheritance does
#include <iostream>
class Vehicle {
public:
void start() const {
std::cout << "Engine on\n";
}
};
class Car : public Vehicle {
public:
void openTrunk() const {
std::cout << "Trunk open\n";
}
};
int main() {
Car car;
car.start(); // inherited from Vehicle
car.openTrunk(); // defined in Car
}
Here Car reuses start() from Vehicle. The relationship is “a car is a vehicle,” and that is inheritance. Nothing in this program is polymorphic, because each call is fixed when the program is compiled.
If the idea of a derived class is new, read “What is inheritance in C++ in simple words?” first.
Inheritance without polymorphism
A derived class can define a function with the same name as one in its base class. If the base function is not virtual, a call through a base reference still runs the base version.
#include <iostream>
class Base {
public:
void hello() const { std::cout << "Base\n"; }
};
class Derived : public Base {
public:
void hello() const { std::cout << "Derived\n"; }
};
int main() {
Derived d;
Base& ref = d;
d.hello(); // prints "Derived"
ref.hello(); // prints "Base", because hello() is not virtual
}
This surprises many beginners. The code uses inheritance, but the call is chosen by the reference type, not by the object it refers to.
Polymorphism with virtual functions
Make the base function virtual, and the same call changes.
#include <iostream>
class Base {
public:
virtual ~Base() = default;
virtual void hello() const { std::cout << "Base\n"; }
};
class Derived : public Base {
public:
void hello() const override { std::cout << "Derived\n"; }
};
int main() {
Derived d;
Base& ref = d;
ref.hello(); // prints "Derived"
}
Now the call goes to the object’s real type. The base class supplies the shared interface, and virtual decides which version runs.
Polymorphism without inheritance
Templates allow the same call to work with unrelated types. Nothing in this example inherits from anything.
#include <iostream>
class Robot {
public:
void hello() const { std::cout << "Beep\n"; }
};
class Person {
public:
void hello() const { std::cout << "Hi\n"; }
};
template <typename T>
void sayHello(const T& thing) {
thing.hello();
}
int main() {
sayHello(Robot{}); // prints "Beep"
sayHello(Person{}); // prints "Hi"
}
Each type gets its own version of sayHello, and the compiler makes that choice. This is polymorphism without inheritance.
Which one to use
- Use inheritance when the types have a real “is a” relationship and should share code or a common interface.
- Use templates when unrelated types support the same operations and you want the compiler to make the choice.
- Keep class hierarchies shallow. Long chains of inheritance are harder to follow and to change.
Neither approach is always the right answer, and many C++ programs use both.