liveBook · Manning

Function overloading

As explained in Object-Oriented Software Design in C++

The definition of multiple functions with the same name but different parameter lists or signatures in a scope.

Function overloading defines two or more functions in the same scope with the same name but different signatures. The C++ compiler selects a function from the call’s arguments. section 7.1.2 section 7.6

Use overloading when functions perform closely related operations:

  • Overload member or standalone functions when separate names would obscure their strong relationship. section 7.1.2
  • Keep overloaded operations conceptually similar, identical, or equivalent. section 7.1.2
  • Distinguish signatures by parameter number, datatypes, or order—not parameter names or return type. section 7.6

One name, different inputs

A drawing interface can offer draw_circle(int x, int y, double radius) and draw_circle(Point p, double radius). Similarly, print(Textfile f) and print(Textfile f, PDF p) express related printing operations.

enum class Kind
{
    FICTION, COOKBOOK, HOWTO
};

inline ostream& operator <<(ostream& ostr, const Kind& k)
{
    switch (k)
    {
        case Kind::FICTION:  ostr << "fiction";  break;
        case Kind::COOKBOOK: ostr << "cookbook"; break;
        case Kind::HOWTO:    ostr << "howto";    break;
    }
    return ostr;
}

Browse an overloaded output operator that converts Kind values into readable strings for cout. section 7.1.2

How Object-Oriented Software Design in C++ covers it

How deep it goes
Explains signatures, compiler selection, and suitable behaviors through compact C++ examples.
Examples
Demonstrates print, draw_circle, search, sqrt, and overloaded operators for arithmetic, strings, and enumerations.
What it assumes
Assumes basic functions and parameters while defining signatures and contrasting overloading with overriding.
What you can do after
Apply function overloading in C++ when related operations need distinct parameter signatures under one name.
How it connects
Connects function overloading with function overriding, operator overloading, signatures, and class design.

Also explained in

Compare how 3 books explain function overloading →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.