In C++, inheritance lets a derived class reuse and extend the members of a base class, while polymorphism lets code call the correct overridden function through a base-class pointer or reference at run time. Inheritance is the relationship between classes. Polymorphism is the behaviour you get when that relationship is combined with virtual functions.
You can use inheritance without polymorphism (non-virtual base classes), but run-time polymorphism through virtual functions needs inheritance.
Inheritance models an "is-a" relationship. The derived class gets the base class's data members and member functions, and can add its own.
Use public inheritance for is-a. Private inheritance models "implemented in terms of", and composition is usually the better tool for that.
Polymorphism means one interface with many behaviours. In C++ it appears as run-time polymorphism (virtual functions) and compile-time polymorphism (templates, overloading). When people compare it with inheritance they almost always mean the run-time kind.
The loop only knows Shape, yet each call runs the derived override. The compiler typically implements this with a hidden vtable pointer in each object, so the call costs one extra indirection.
| Aspect | Inheritance | Polymorphism |
|---|---|---|
| What it is | A class relationship (is-a) | A behaviour: one call, many implementations |
| Main goal | Code reuse and extension | Flexibility and extensibility |
| Mechanism | class D : public B | virtual, override, base pointer or reference |
| Resolved | Layout at compile time | Function chosen at run time |
| Works without the other | Yes, with non-virtual members | No, run-time polymorphism needs inheritance |
Not all polymorphism needs a class hierarchy. Overloading, templates and concepts (C++20) give static polymorphism with no vtable cost.
Square and Disc share no base class, yet print_area works with both. Choose this when the types are known at compile time and you want inlining. Choose virtual functions when the type is only known at run time, for example objects loaded from a plugin.
override. A tiny signature mismatch (such as a missing const) silently creates a new function instead of overriding.Derived by value as a Base copies only the base part and loses the overrides. Pass by reference or pointer.final.Yes, through templates, concepts and function overloading (static polymorphism), or std::function and std::variant with std::visit. Only run-time polymorphism through virtual functions requires inheritance.
Yes. If no functions are virtual, calls resolve at compile time by the static type and you simply reuse the base class's code.
Overloading uses the same function name with different parameters in one scope and is resolved at compile time. Overriding replaces a base class virtual function in a derived class and is resolved at run time.
A virtual call adds an indirection and can block inlining, but the cost is small in most programs. Measure before replacing virtual functions with templates for speed.
Inheritance is how classes share and extend structure. Polymorphism is how a single base-class interface runs different code for different objects, using virtual functions and override. Use a virtual destructor in base classes, pass polymorphic objects by pointer or reference, and consider templates and concepts when you do not need run-time dispatch.