1.
Can a function return a reference?
2.
What is an independent reference?
3.
Can you create a reference to a reference?
CRITICAL SKILL 6.7: Function Overloading
In this section, you will learn about one of C++’s most exciting features: function overloading. In C++,
two or more functions can share the same name as long as their parameter declarations are different. In
this situation, the functions that share the same name are said to be overloaded, and the process is
referred to as function overloading. Function overloading is one way that C++ achieves polymorphism.
In general, to overload a function, simply declare different versions of it. The compiler takes care of the
rest. You must observe one important restriction: the type and/or number of the parameters of each
overloaded function must differ. It is not sufficient for two functions to differ only in their return types.
They must differ in the types or number of their parameters. (Return types do not provide sufficient
information in all cases for C++ to decide which function to use.) Of course, overloaded functions may
differ in their return types, too. When an overloaded function is called, the version of the function
whose parameters match the arguments is executed.
14
C++ A Beginner’s Guide by Herbert Schildt
Let’s begin with a short sample program:
This program produces the following output:
As you can see, f( ) is overloaded three times. The first version takes one integer parameter, the second
version requires two integer parameters, and the third version has one double parameter. Because the
parameter list for each version is different, the compiler is able to call the correct version of each
function based on the type of the arguments specified at the time of the call. To understand the value of
function overloading, consider a function called neg( ) that returns the negation of its arguments. For
example, when called with the value –10, neg( ) returns 10. When called with 9, it returns –9. Without
15
C++ A Beginner’s Guide by Herbert Schildt
function overloading, if you wanted to create negation functions for data of type int, double, and long,
you would need three different functions, each with a different name, such as ineg( ), lneg( ), and fneg(
). However, through the use of function overloading, you can use one name, such as neg( ), to refer to all
functions that return the negation of their argument. Thus, overloading supports the polymorphic
concept of “one interface, multiple methods.” The following program demonstrates this:
The output is shown here:
neg(-10): 10
neg(9L): -9
16
C++ A Beginner’s Guide by Herbert Schildt
neg(11.23): -11.23
This program creates three similar but different functions called neg, each of which returns the absolute
value of its argument. The compiler knows which function to use in each given situation because of the
type of the argument.
The value of overloading is that it allows related sets of functions to be accessed using a common name.
Thus, the name neg represents the general action that is being performed. It is left to the compiler to
choose the right specific version for a particular circumstance. You, the programmer, need only
remember the general action being performed. Therefore, through the application of polymorphism,
three things to remember have been reduced to one. Although this example is fairly simple, if you
expand the concept, you can see how overloading can help you manage greater complexity.
Another advantage to function overloading is that it is possible to define slightly different versions of the
same function that are specialized for the type of data upon which they operate. For example, consider
a function called min( ) that determines the minimum of two values. It is possible to create versions of
min( ) that behave differently for different data types. When comparing two integers, min( ) returns the
smallest integer. When two characters are compared, min( ) could return the letter that is first in
alphabetical order, ignoring case differences. In the ASCII sequence, uppercase characters are
represented by values that are 32 less than the lowercase letters. Thus, ignoring case would be useful
when alphabetizing. When comparing two pointers, it is possible to have min( ) compare the values
pointed to by the pointers and return the pointer to the smallest value. Here is a program that
implements these versions of min( ):
17
C++ A Beginner’s Guide by Herbert Schildt
When you overload a function, each version of that function can perform any activity you desire. That is,
there is no rule stating that overloaded functions must relate to one another. However, from a stylistic
point of view, function overloading implies a relationship. Thus, while you can use the same name to
overload unrelated functions, you should not. For example, you could use the name sqr( ) to create
functions that return the square of an int and the square root of a double. These two operations are
fundamentally different, however, and applying function overloading in this manner defeats its original
purpose. (In fact, programming in this manner is considered to be extremely bad style!) In practice, you
should overload only closely related operations.
Automatic Type Conversions and Overloading
As you will recall from Module 2, C++ provides certain automatic type conversions. These conversions
also apply to parameters of overloaded functions. For example, consider the following:
18
C++ A Beginner’s Guide by Herbert Schildt
In this example, only two versions of f( ) are defined: one that has an int parameter and one that has a
double parameter. However, it is possible to pass f( ) a short or float value. In the case of short, C++
automatically converts it to int. Thus, f(int) is invoked. In the case of float, the value is converted to
double and f(double) is called.
It is important to understand, however, that the automatic conversions apply only if there is no direct
match between a parameter and an argument. For example, here is the preceding program with the
addition of a version of f( ) that specifies a short parameter:
19
C++ A Beginner’s Guide by Herbert Schildt
Now when the program is run, the following output is produced:
Inside f(int): 10
Inside f(double): 10.1
Inside f(short): 99
Inside f(double): 11.5
In this version, since there is a version of f( ) that takes a short argument, when f( ) is called with a short
value, f(short) is invoked and the automatic conversion to int does not occur.
Do'stlaringiz bilan baham: |