summaryrefslogtreecommitdiff
path: root/chapter6/maximal.cxx
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-04-07 23:18:32 -0700
committerFuwn <[email protected]>2024-04-07 23:18:32 -0700
commitc1b6ffe70bd281c6c230fd63fabcaac2aff47514 (patch)
treee8af3b1782a7cd0754590ed618fddc1bdb9b7385 /chapter6/maximal.cxx
downloaddscode-main.tar.xz
dscode-main.zip
feat: initial commitHEADmain
Diffstat (limited to 'chapter6/maximal.cxx')
-rw-r--r--chapter6/maximal.cxx36
1 files changed, 36 insertions, 0 deletions
diff --git a/chapter6/maximal.cxx b/chapter6/maximal.cxx
new file mode 100644
index 0000000..aecd5a9
--- /dev/null
+++ b/chapter6/maximal.cxx
@@ -0,0 +1,36 @@
+// FILE: maximal.cxx
+// A demonstration program for a template function called maximal.
+
+#include <cstdlib> // Provides EXIT_SUCCESS
+#include <iostream> // Provides cout
+#include <string> // Provides string class
+using namespace std;
+
+// TEMPLATE FUNCTION used in this demonstration program:
+// Note that some compilers require the entire function definition to appear
+// before its use (rather than a mere prototype). This maximal function is
+// similar to max from <algorithm>.
+template <class Item>
+Item maximal(Item a, Item b)
+// Postcondition: Returns the larger of a and b.
+// Note: Item may be any of the C++ built-in types (int, char, etc.), or a
+// class with the > operator and a copy constructor.
+{
+ if (a > b)
+ return a;
+ else
+ return b;
+}
+
+int main( )
+{
+ string s1("frijoles");
+ string s2("beans");
+
+ cout << "Larger of frijoles and beans: " << maximal(s1, s2) << endl;
+ cout << "Larger of 10 and 20 : " << maximal(10, 20) << endl;
+ cout << "Larger of $ and @ : " << maximal('$', '@') << endl;
+ cout << "It's a large world." << endl;
+
+ return EXIT_SUCCESS;
+}