#include #include using namespace std; class Foo{ public: Foo(string n = "Dan", int a = 10): name(n), age(a) {}; friend ostream & operator << (ostream & s, const Foo & other) { s << other.name << " " << other.age; return s; } bool operator <(const Foo & other) const { if (name == other.name) { return age < other.age; } return name < other.name; } private: string name; int age; }; // in the header file // or at the top of the code. no function prototype template bool Less(const T & a, const T & b) { return a < b; } template void LessTest(myType a, myType b) { cout << "Comparing " << a << " to " << b << endl; if (Less(a,b)) { cout << "\t" << a << " is smaller." << endl; } else { cout << "\t" << b << " is smaller." << endl; } } template T Smaller(T a, T b) { T rValue = b; if (Less(a,b)) { rValue = a; } return rValue; } int main() { string word1{"Hello"}; string word2{"World"}; /* if (Less(3,5)) { cout << "3 < 5 " << endl; } else { cout << "5 < 3 " << endl; } if (Less("Hello","World")) { cout << "Hello < Word " << endl; } else { cout << "Word < Hello" << endl; } if (Less(word1,word2)) { cout << word1 << " < " << word2 << endl; } else { cout << word2 << " < " << word1 << endl; } */ LessTest(3,5); LessTest(5,3); LessTest(word1, word2); LessTest(word2, word1); /* LessTest(3, 3.14); LessTest(3.14, 3); LessTest(3, 2.14); LessTest(2.14, 3); */ Foo first; Foo second("Adam", 5); Foo third ("Paul", 7); LessTest(first, second); LessTest(first, third); cout << "The smaller of " << first << " and " << second << " is " << Smaller(first, second) << endl; return 0; }