home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch12 / ovrload1.cpp < prev    next >
C/C++ Source or Header  |  2005-06-16  |  1KB  |  53 lines

  1. //             ovrload1.cpp
  2. //
  3. // Synopsis  - Displays the sum of two double variables 
  4. //             and the sum of the first four elements
  5. //             of an array.
  6. //
  7. // Objective - To demonstrate overloaded functions.
  8. //
  9.  
  10. // Include Files
  11. #include <iostream.h>
  12. // Function Prototypes
  13. double sum( double a, double b );                    // Note 1
  14. // PRECONDITION:  none.
  15. //
  16. // POSTCONDITION: the return value is the sum of a and b.
  17.  
  18. int sum( int array[], int n );                       // Note 2
  19. // PRECONDITION:  array must have n or more cells
  20. //
  21. // POSTCONDITION: the return value is the sum of the
  22. //                first n cells in array.
  23.  
  24. int main()
  25. {
  26.     int an_array[] = { 3, 5, 7, 12, 2, 15, 4, 8 };
  27.     double x = 53.12, y = 45.872;
  28.  
  29.     cout << "The sum of x and y is " 
  30.          << sum( x, y ) << endl;                     // Note 3
  31.  
  32.     cout << "The sum of the first four elements of an_array is "
  33.          << sum( an_array, 4 ) << endl;              // Note 4
  34.  
  35.     return 0;
  36. }
  37.  
  38. /*************************** sum( double, double ) **********/
  39. double sum( double a, double b )                     // Note 1
  40. {
  41.     return ( a + b );
  42. }
  43.  
  44. /*************************** sum( int[], int ) **************/
  45. int sum( int array[], int n )                        // Note 2
  46. {
  47.     int the_sum = 0;
  48.  
  49.     for ( int i = 0; i < n; i ++ )
  50.         the_sum += array[i];
  51.  
  52.     return the_sum;
  53. }