// ----------------------------- // template.cpp // ----------------------------- #include "w.h" #include "complex.h" #include "complex.cpp" #include "frac.h" #include "frac.cpp" // define the operator > for 'our' Complex class int operator > (const Complex &c1, const Complex &c2) { return (norm(c1) > norm(c2) ? 1 : 0); } // define the function Max for *any* class having operator > template X Max(X a, X b) { return (a > b ? a : b); } // ----------------------------------- void main() { nl(0); banner("template.cpp"); pl("Max(1,2) = ",Max(1,2)); pl("Max(3.4,3.2) = ",Max(3.4,3.2)); Complex a(1,2),b(2,0.9); p("a = "); p(a); p(" b = "); pl(b); p("Max(a,b) = "); pl( Max(a,b)); frac f1(4,5),f2(41,51); p("f1 = "); p(f1); p(" f2 = "); pl(f2); p("Max(f1,f2) = "); pl(Max(f1,f2)); } /* output ------------------------------------ -------------- template.cpp -------------- Max(1,2) = 2 Max(3.4,3.2) = 3.4 a = (1, 2) b = (2, 0.9) Max(a,b) = (1, 2) f1 = 4/5 f2 = 41/51 Max(f1,f2) = 41/51 */