1
0
Fork 0
This repository has been archived on 2021-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
uni-m.cds-adv-prog/ex2.2/overloading.cpp

75 lines
1.3 KiB
C++
Raw Normal View History

/*
* Function Overloading
*
* generalisation of type is possible
* => not writing an int implementation means always use the double
* => => even when two ints are supplied, they are generalised to doubles
*/
#include <iostream>
int min( int a, int b );
double min( double a, int b );
double min( int a, double b );
double min( double a, double b );
int min( int a[], int size);
int main() {
const int size = 5;
int list[size] = { 42, 37, 29, 23, 19 };
std::cout << "min(5, 1) = " << min(5,1) << std::endl;
std::cout << "min([42,37,29,23,19],5) = " << min(list,size) << std::endl;
std::cout << "min( 3.12345, 3.15432 ) = " << min(3.12345, 3.15432) << std::endl;
std::cout << "min( 3.12345, 4 ) = " << min(3.12345, 4) << std::endl;
std::cout << "min( 4, 3.12345 ) = " << min(4, 3.12345) << std::endl;
return 0;
}
int min( int a, int b ) {
if ( a < b ) {
return a;
}
return b;
}
double min( double a, int b ) {
if ( a < b ) {
return a;
}
return b;
}
double min( int a, double b ) {
if ( a < b ) {
return a;
}
return b;
}
double min( double a, double b ) {
if ( a < b ) {
return a;
}
return b;
}
int min( int a[], int size ) {
int m = a[0];
for( int i = 1; i < size ; ++i ) {
m = min(a[i], m);
}
return m;
}
// Fix interchangeabiliy of doubles and ints