C++. Overloaded functions. Overloaded functions in classes. Overloading of class constructors. Access to the overloaded function using a pointer. Examples




Overloaded functions. Overloaded functions in classes. Overloading of class constructors. Access to the overloaded function using a pointer. Examples


Contents


Search other websites:

1. What functions are called “overloaded”? What does the term “overload” mean?

The “overload” of a function is the declaration of a function with the same name several times. Thus, in some scope, the name of the “overloaded” function is declared several times. In order for the compiler to distinguish between “overloaded” functions among themselves, these functions must be distinguished by a list of input parameters.

In general, the declaration of an overloaded function in a certain scope is as follows:

return_type1 FunName(parameters_list_1)
{
    // ...
}

return_type2 FunName(parameters_list_2)
{
    // ...
}

return_typeN FunName(parameters_list_N)
{
    // ...
}

where

  • FunName – the name of overloaded function;
  • parameters_list1, parameters_list2, …, parameters_listN – lists of parameters of the “overloaded” function with the name FunName;
  • return_type1, return_type2, …, return_typeN – the types of parameters that are returned by the “overloaded” function FunName. The compiler distinguishes between “overloaded” functions only from the list of received parameters and not from the returned value.

2. What are the signs of overloaded functions? Example

Overloaded functions differ based on the parameters list. Lists of parameters for overloaded functions should differ according to the following characteristics:

  • number of parameters;
  • if the number of parameters is the same, then by the types of parameters.

For example. Max() function is overloaded and has different number of parameters and parameter types

// Function Max() with two parameters of type int
int Max(int a, int b)
{
    // ...
}

// Function Max() with three parameters of type int
int Max(int a, int b, int c)
{
    // ...
}

// Function Max() with two parameters of type double
int Max(double a, double b)
{
    // ...
}

3. Examples of function overloading

Example 1. The function Equal(), which compares two values of different types. The function has three overloaded implementations for different types (char, int, double).

The code that demonstrates the use of the Equal() function is:

#include "stdafx.h"
#include <iostream>
using namespace std;

bool Equal(char c1, char c2)
{
    return (bool)(c1==c2);
}

bool Equal(double d1, double d2)
{
    return (bool)(d1==d2);
}

bool Equal(int i1, int i2)
{
    return (bool)(i1==i2);
}

int _tmain(int argc, _TCHAR* argv[])
{
    bool b;
    b = Equal(5,6); // Equal(int, int), b = 0
    b = Equal(3.755, 3.755); // Equal(double, double), b = 1
    b = Equal('A', 'A'); // Equal(char, char), b = 1
    return 0;
}

Example 2. Function Average(), which finds the arithmetic mean. The function has 4 overloaded implementations for a different number of integer parameters.

The program code that demonstrates using of the function is as follows:

#include "stdafx.h"
#include <iostream>
using namespace std;

double Average(int a, int b)
{
    return (a+b)/2.0;
}

double Average(int a, int b, int c)
{
    return (a+b+c)/3.0;
}

double Average(int a, int b, int c, int d)
{
    return (a+b+c+d)/4.0;
}

double Average(int a, int b, int c, int d, int e)
{
    return (a+b+c+d+e)/5.0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    double avg;
    avg = Average(2,3); // avg = 2.5
    avg = Average(2,3,5); // avg = 3.3333
    avg = Average(2,3,5,8); // avg = 4.5
    avg = Average(2,3,5,8,11); // avg = 5.8
    return 0;
}

4. Example of overloading the function in a class

Let the Day class, which realizes the day of the week, be given. The Set() function is overloaded in the class, which sets a new day of the week. The Set() method has 2 implementations:

  • without parameters;
  • with used one parameter.

The program code that demonstrates using of the “overloading” functions in the class has the following form:

#include "stdafx.h"
#include <iostream>
using namespace std;

class Day
{
    int day;

    public:
    // method Set() is overloaded
    void Set(int nday) { day = nday; }
    void Set(void) { day = 1; }
    int Get(void) { return day; }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Day D;
    int day;

    // the use of overloaded Set() method
    D.Set();
    day = D.Get(); // day = 1

    D.Set(5);
    day = D.Get(); // day = 5

    cout << day << endl;
    return 0;
}

5. Can functions that have the same names, the same number and types of parameters be considered to be overloaded, but which return values of different types?

No. The compiler recognizes overloaded functions only on the parameters received. If two functions have the same names, the same number and types of parameters and they return different values, then such functions are assumed to be the same. In this case, the compiler will generate an error:

For example. The following code declares two functions named Inc5():

int Inc5(int d)
{
    return d+5;
}

double Inc5(int d)
{
    return (double)(d+5.0);
}

As you can see from the example, functions return value of different types. Because the functions have the same names, the same number and types of parameters, the above code will produce a compiler error

'double Get5(int)' : overloaded function differs only by return type from 'int Get5(int)'

6. Why is class constructor overloading used? The advantages of overloading class constructors

Overloading class constructors gives the following advantages:

  • the increasing of flexibility of the class when creating it. With the help of constructors overload, the user chooses the best way to create an object. If you try to create a class object in a way for which the constructor is unexpected, the compiler will display an error message;
  • the ability to create initialized and uninitialized objects or objects that are initialized by default. In this case, there are 2 possible implementations of the constructor: with and without initialization;
  • the ability to create copy constructors.





7. Example of overloading of constructors in a class

Example. The Cylinder class that implements the cylinder is specified. In the class, the constructor is overloaded, which can be called in one of three ways:

  • constructor without parameters;
  • constructor with 1 parameter;
  • constructor with 2 parameters.
#include "stdafx.h"
#include <iostream>
using namespace std;

class Cylinder
{
    double r, h;
    public:
    // constructor without parameters
    Cylinder()
    {
        r = 1;
        h = 1;
    }

    // constructor with 1 parameter
    Cylinder(double h)
    {
        r = 1.0;
        this->h = h;
    }

    // constructor with two parameters
    Cylinder(double h, double r)
    {
        this->h = h;
        this->r = r;
    }

    // access methods
    double GetR(void) { return r; }
    double GetH(void) { return h; }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Cylinder cl; // the constructor without parameters is invoked
    Cylinder cl1(5); // the constructor with 1 parameter is invoked
    Cylinder cl2(7, 9); // the constructor with 2 parameters is invoked
    double d;

    d = cl.GetH(); // d = 1
    d = cl1.GetH(); // d = 5
    d = cl2.GetH(); // d = 7

    cout << d << endl;
    return 0;
}

8. How is realized access to the overloaded function with a function pointer? Example

When you declare a pointer to an “overloaded” function, the compiler determines the desired function for the pointer by its declaration.

For example. Let there be given a three “overloaded” the functions Increment(), which operate the types int, double, char. When you declare a pointer to a function, you must explicitly set the type of the pointer when it is declared.

The following code demonstrates the use of a pointer to an overloaded function

#include "stdafx.h"
#include <iostream>
using namespace std;

int Increment(int i)
{
    return i+1;
}

double Increment(double d)
{
    return d+1;
}

char Increment(char c)
{
    return c+1;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int (*pi)(int); // pointer to a function that gets int and returns int
    pi = Increment; // p points to: int Increment(int)
    int d = (*pi)(4); // d = 5
    char (*pc)(char); // a pointer to a function that operates with the 'char' type
    pc = Increment; // p points to: char Increment(char)
    char c = (*pc)('F'); // c = 'G'
    cout << c << endl;
    return 0;
}

9. Why is the ‘overload’ keyword used?

The ‘overload’ keyword was used in earlier versions of C ++ to indicate that the function is overloaded. The general form of declaring an “overloaded” function using the ‘overload’ keyword is:

overload name_of_function;

For example. Suppose there is an overloaded function Max() that finds a maximum between a set of numbers. In this case, the string

overload Max;

notifies the compiler that the Max() function is overloaded.


Related topics