Functions overloading

In C++, it’s quite common to have several functions with same name, though they must have a different list of arguments. For instance, one can have the following three functions to compute the maximum of two or more values. Note that std::string is used to represent text like words (and usually called strings in programming).

// Return the largest of x and y
double max(double x, double y) {
    if (x < y) {
        return x;
    }
    return y;
}

// Return the maximum number stored in V
double max(const std::vector<double>& V) {
    double result = V[0];

    for (double x : V) {
        if (x > result) {
            result = x;
        }
    }
    return result;
}

// Return the lexicographically largest word stored in V
std::string max(const std::vector<std::string>& V) {
   std::string result = V[0];

    for (std::string word : V) {
        if (word > result) { // lexicographical order is used
            result = word;
        }
    }
    return result;
}

It’s natural that all functions above have the same name because they all conceptually do the same task, i.e. compute the largest of two or more given values. However, the functions differ on the type and number of arguments they accept. The first function expects two doubles as arguments, while the second and third functions expect a vector of doubles and a vector of strings, respectively.

int main() {
    std::vector<std::string> words_list = {"Theoder", "Ebba", "Mark", "Anna", "Isak"};
    std::cout << max(words_list) << "\n";
	
    std::cout << max(-5, 10.2);
}

The function call max(words_list) calls the third function max given above because the actual argument words_list used in the function call has type std::vector<std::string> which is compatible with the type of the formal parameter of the third function.

The functions max given above can all exist in the same program. This is an example that illustrates the concept of function overloading. The compiler has no problem to understand which function is being called, though they have the same name. The compiler also looks at the type of the actual arguments used in the function call, to be able to decide which of the three functions max is being called.

Good programming practice: overloaded functions (i.e. several functions in a program with same name) should conceptully do the same task.

Function overloading is used quite often in the C++-standard library. See, for instance, the function std::getline to read a line of text.

Exercise

The function max given above, which returns the lexicographically largest word stored in a given vector V, disregards that the words may occur as a mix of upper-case and lower-case letters. Re-write the function so that it finds the lexicographically largest word, taking into consideration that words my have upper-case and lower-case letters. Thus, for instance, the words “english” and “English” should be considered as equal and “Swedish” should be lexicographically larger than “english”. The function is given again below.

// Return the lexicographically largest word stored in V
std::string max(const std::vector<std::string>& V) {
   std::string result = V[0];

    for (std::string word : V) {
        if (word > result) { // lexicographical order is used
            result = word;
        }
    }
    return result;
}