forked from apanangadan/CSUF-CPSC_131
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTemplates.cpp
More file actions
106 lines (89 loc) · 3.03 KB
/
Templates.cpp
File metadata and controls
106 lines (89 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Code from Kevin Wortman's lecture in week 3
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
// First suppose we want to write functions for minimums of arrays. We
// could copy-paste code to write similar functions like so:
int MinimumOfInts(int array[], int count) throw(invalid_argument, length_error) {
if (array == nullptr) {
throw invalid_argument("minimum array must not be null");
} else if (count < 1) {
throw length_error("minimum array must be non-empty");
} else {
int smallest = array[0];
for (int i = 1; i < count; i++) {
if (array[i] < smallest)
smallest = array[i];
}
return smallest;
}
}
double MinimumOfDoubles(double array[], int count) throw(invalid_argument, length_error) {
if (array == nullptr) {
throw invalid_argument("minimum array must not be null");
} else if (count < 1) {
throw length_error("minimum array must be non-empty");
} else {
double smallest = array[0];
for (int i = 1; i < count; i++) {
if (array[i] < smallest)
smallest = array[i];
}
return smallest;
}
}
// However this copy-pasting violates the Don't Repeat Yourself (DRY)
// prinicple and is tedious and error-prone.
// It's better to define a single template function so that the
// compiler can "fill in the blanks" to synthesize these functions for
// us.
template <typename NUMBER>
NUMBER Minimum(NUMBER array[], int count) throw(invalid_argument, length_error) {
if (array == nullptr) {
throw invalid_argument("minimum array must not be null");
} else if (count < 1) {
throw length_error("minimum array must be non-empty");
} else {
NUMBER smallest = array[0];
for (int i = 1; i < count; i++) {
if (array[i] < smallest)
smallest = array[i];
}
return smallest;
}
}
// We can do the same thing for a template class.
template <typename ELEMENT>
class TwoOf {
private:
ELEMENT first, second;
public:
TwoOf(ELEMENT first, ELEMENT second)
: first(first), second(second) { }
ELEMENT getFirst() { return first; }
ELEMENT getSecond() { return second; }
void setFirst(ELEMENT first) { this->first = first; }
void setSecond(ELEMENT first) { this->second = second; }
};
int main() {
// Demo instantiating template function
float fractions[4] = { 201.6, 33.8, 409.1, 66.9 };
// This instantiates Minimum substituting ELEMENT=float
cout << Minimum(fractions, 4) << endl; // prints out 33.8
char characters[9] = "platypus"; // length is 9 to leave space for
// the null terminator
// This instantiates Minimum substituting ELEMENT=char
cout << Minimum(characters, 8) << endl; // prints out a
// Demo instantiating template class
// Instantiate TwoOf substituting ELEMENT=double
TwoOf<double> a(3.7, 9.1);
// Instantiate TwoOf substituting ELEMENT=string
TwoOf<string> b("cat", "dog");
// prints 3.7 9.1 cat dog
cout << a.getFirst() << ' '
<< a.getSecond() << ' '
<< b.getFirst() << ' '
<< b.getSecond() << endl;
return 0;
}