Polymorphism and Overloading in C++

A reader sent me an interesting question the other day. They asked if polymorphism and overloading were essentially the same thing.

My initial reaction was Huh?

What are people being taught if they think that these two concepts are the same thing?

But a quick google search revealed that yes, many, many people are struggling to differentiate between these two terms – and the internet is full of conflicting and unhelpful information.

Not only that, but I found this statement (copied and pasted all over the web) from a book called “Object Oriented Programming in C++”:

Overloading is a kind of polymorphism; it is also an important feature of C++.

Now, I’m sure this is a very good book (I haven’t read it, so I can’t comment. Maybe I will read it. Then I can.) but this statement is BOUND to cause unnecessary confusion for C++ learners.

There is no explanation of why overloading is a kind of polymorphism, or how indeed polymorphism relates to overloading. It’s the kind of throwaway statement that I really hate to see in textbooks (in which I believe that everything should be clearly stated and clearly explained with zero ambiguity).

So what’s the answer?

The basic answer is no, they are NOT the same – but they are related.

Polymorphism in C++ describes the ability of different objects to be accessed by a common interface

I’ve written about polymorphism before here.

Whereas overloading in C++ allows the compiler to determine which version of a method is to be called based on the parameters that are passed to it.

I’ve written about overloading (versus overriding) before here.

So, just to be clear:

Polymorphism relates to objects.

Overloading relates to methods.

Polymorphism is how we subclass different types of objects (e.g. drinks: tea, coffee, champagne).

Overloading is how we accommodate variations in instructions (e.g. serve tea (the object) on its own, serve tea with a biscuit, serve tea with cake).

But…

There is more to the story.

Because, academically, overloading is categorised as polymorphism.

WTF?

I know, I know, I just told you they aren’t the same, and now I’m confusing you.

Ignore, for a minute, what polymorphism means in C++ or Java, and think of it for now, as a higher level idea.

Poly = many

Morph = form

It’s an umbrella term for using the same interface for different behaviours.

The polymorphism that you know in C++ is actually, more accurately termed inclusion or subtype polymorphism.

In fact, I drew a nice diagram to save having to bandy all these terms around, because sometimes things are just much easier to explain visually:

polymorphism

So, in a nutshell:

  • Polymorphism and overloading (in C++) are two separate concepts. They are not the same.
  • Polymorphism in C++ is more specifically termed “inclusion polymorphism” or “subtype polymorphism”.
  • Overloading in C++ is a type of polymorphism, called ad hoc polymorphism.

So, going back to the quote in the book – it is entirely correct, but causes confusion because the author does not distinguish between polymorphism as a general concept, and polymorphism as used to create sub classes in C++.

And going back to the original question, polymorphism and overloading are not the same, but they both utilise a polymorphic approach.

5 thoughts on “Polymorphism and Overloading in C++”

  1. In C++ polymorphism (done with virtual functions) is also a ‘dynamic’ thing. That means, in a runtime there is a decision made which function to call.

    Overloading is a static thing, done at compile time.

    • True. Templates are also a kind of polymorphism (parametric) and they are statically polymorphic too, so dynamic v static isn’t the differentiating point, just part of how that particular polymorphism is implemented.

  2. This is why many soft workers prefer to work with professional languages like VB. There is no issue of problem with overloading definitions, and soft runs very fast and can work directly to the spreadsheet without having use open source (freeware products) which sometime cannot be trust.

  3. I find the first part of your article slightly misleading, because I don’t think it is useful to constrain the term polymorphism in C++ to inheritance and virtual member functions.
    While it is not wrong (on the contrary, the C++ standard defines polymorphic classes as those with virtual functions (§10.3/1), and std::is_polymorphic does return true for exactly these classes) most people have gone over to the definitions you use in the second part of the article. To differentiate what you called subtype polymorhism from the more general concept people do in fact speak of subtype, runtime or dynamic polymorphism, and do in fact think of overloading as another type of (static) polymorphism.

    There is that one very interesting talk by Sean Parent (https://www.youtube.com/watch?v=_BpMYeUFXv8), that gives a great perspective about how we can overcome many of the problems with subtype polymorphism by implementing solutions that make use of the broader definition.

  4. You have such a wonderful and crytal clear way of writing. Thank you, and keep writing!

Comments are closed.