/* types.c++ * * In the C++ language, a successor to C, the idea of polymorphic dipatch * was given a boost with the "template" syntax, declaring a family of * functions (or objects) with differing types. * * So here we are allowed to have two different plus() functions, * as long as we declare ahead of time the "template" for them. * * $ gcc types3.c++ -o types3 # compile at C++ program * $ ./types3 # run it * -- int plus -- * 3 + 4 is 7 * -- float plus -- * 3.2 + 4.1 is 7.3 * */ #include template T plus(T a, T b); int plus(int a, int b){ printf(" -- int plus -- \n"); return a + b; } float plus(float a, float b){ printf(" -- float plus -- \n"); return a + b; } int main(){ int i=3; int j=4; float x=3.2; float y=4.1; printf(" %d + %d is %d \n", i, j, plus(i,j)); printf(" %g + %g is %g \n", x, y, plus(x,y)); return 0; }