5.1.1. Exercises: Overloading Functions

  1. Experiment with Example 5.1. Start by uncommenting the third member function and compiling.

  2. Try uncommenting the following line just before the end of main():

       //  csd.demo(4.5);  
       

    What happened? Explain the error message.

     

    We get an ambiguous function-call message like this:

        
    function-call.cpp:43: error: call of overloaded 'demo(double)' is ambiguous
     void SignatureDemo::demo(int) <near match>
     Candidates are: void SignatureDemo::demo(int) const
                     void SignatureDemo::demo(short int) <near match>
                     void SignatureDemo::demo(float) <near match>
                     void SignatureDemo::demo(float) const
                     void SignatureDemo::demo(double) <near match>
    

    There are many possibilities listed because this is not a promotion, but a demotion, and there are two possible ways to convert with loss of data: to int or float. In addition, there are near match candidates which break const rules but are listed as possibilities anyway.

  3. Notice the compiler warnings about unused parameters when you build this application. Try deleting all the unused parameter names from the function heads that the compiler mentions. (Do not delete the parameter types!) Then rebuild the application and observe the compiler output. This is a good way to notify the compiler that you do not intend to use certain parameters. You can see this technique used in a more convincing way in Section 15.2.

  4. Add other function calls and other variations on the demo() function.

    Explain each result.

[ fromfile: functions.xml id: None ]