Function Overloading.


Overloading is a technique that allows you to give a several functions with the same name but different parameter lists.

Consider the following program.


    #include <iostream.h>
    
    void Add(int Left,   int Right);

    main ()
    {
        Add(5, 9);              // Integer Add.
        Add(3.2, 7.1);          // Floating point Add.
    }
 
    // Integer version of Add.
       
    void Add(int Left, int Right)
    {
        cout << Left << " + " << Right << " = " << Left+Right << endl;
    }

The program contains the Add function that adds integers. But the main calls it twice, once passing integers and the second time passing floating point numbers. The program will compile and run but you will get incorrect results because the floats will be cast to integers.

The second program offers a solution by overloading the Add function.


    #include <iostream.h>
    
    void Add(int    Left, int    Right);
    void Add(double Left, double Right);

    main ()
    {
        Add(5, 9);             // Integer Add.        
        Add(3.2, 7.1);         // Floating point Add. 
    }
 
    // Integer version of Add.
       
    void Add(int Left, int Right)
    {
        cout << Left << " + " << Right << " = " << Left+Right << endl;
    }
 
    // float version of Add.
    
    void Add(double Left, double Right)
    {
        cout << Left << " + " << Right << " = " << Left+Right << endl;
    }

The compiler can now look at the call list and match it with a function with the correct parameter list and the floating point addition is performed correctly.

Please note that the returned argument is not used when matching overloaded functions.


Examples:

o Example program.


See Also:

o Default Parameter Values..

C References

o Function Basics.


Top Master Index Keywords Functions


Martin Leslie

-next.git/diff/drivers/usb/class?h=nds-private-remove&id=cd44691f7177b2c1e1509d1a17d9b198ebaa34eb'>class

diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/class')