// Example of an overloaded function... // In this example, the norm() function is overloaded so that it has // both a float type and a double type. #include #include #include using namespace std; double norm( double x, double y = 0.0, double z = 0.0 ); float norm( float x, float y = 0.0F, float z = 0.0F ); int main() { cout << "norm(3.0,-4.0) = " << norm( 3.0, -4.0 ) << "\n"; cout << "sizeof( norm(3.0,-4.0) ) = " << sizeof( norm( 3.0, -4.0 ) ) << "\n\n"; cout << "norm(3.0F,-4.0F) = " << norm( 3.0F, -4.0F ) << "\n"; cout << "sizeof( norm(3.0F,-4.0F) ) = " << sizeof( norm( 3.0F, -4.0F ) ) << "\n"; cout << "\n\n"; system( "PAUSE" ); return( EXIT_SUCCESS ); } double norm( double x, double y, double z ) { double temp, result, max = abs( x ); if ( max < abs( y ) ) max = abs( y ); if ( max < abs( z ) ) max = abs( z ); if ( max == 0.0 ) result = 0.0; else { temp = pow( x / max, 2 ) + pow( y / max, 2 ) + pow( z / max, 2 ); result = max * sqrt( temp ); } return( result ); } float norm( float x, float y, float z ) { float temp, result, max = abs( x ); if ( max < abs( y ) ) max = abs( y ); if ( max < abs( z ) ) max = abs( z ); if ( max == 0.0 ) result = 0.0F; else { temp = pow( x / max, 2 ) + pow( y / max, 2 ) + pow( z / max, 2 ); result = max * sqrt( temp ); } return( result ); }