-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassTemplates.cpp
More file actions
102 lines (80 loc) · 2.07 KB
/
ClassTemplates.cpp
File metadata and controls
102 lines (80 loc) · 2.07 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
#include <iostream>
#include <assert.h>
// Compiler: Microsoft Visual C++ Compiler Nov 2012 CTP (v120_CTP_Nov2012)
using namespace std;
// Use a class template to define an abstract type
// whose behaviour is generic and is reusable, adaptable.
template<class T>
class Item
{
public:
Item() : Data( T() )
{}
void SetData(T nValue)
{
Data = nValue;
}
void PrintData()
{
cout << Data << endl;
}
void PrintDataOnlyWorksForInt()
{
cout << Data % 10 << endl;
}
private:
T Data;
};
// SIZE is a non-type template argument.
// Can only use int, char, long, long long, unsigned, and enums.
template<class T, int SIZE>
class Array
{
static const int Elements_2x = SIZE * 2; // Use static-constant values to initialize the class.
public:
void Initialize()
{
for(int nIndex = 0; nIndex < SIZE; ++nIndex)
TheArray[nIndex] = T();
}
void GetDefaultValue(int arg = SIZE); // use static-constant value to set a default parameter of a function.
// Array-element access operator to get/read the element.
T operator[](int nIndex) const
{
if (nIndex > 0 && nIndex < SIZE)
{
return TheArray[nIndex];
}
return T(); // Return default value for type
}
T& operator[](int nIndex) // Returns a reference of an element which can be modified by the caller.
{
assert(nIndex > 0 && nIndex < SIZE)
return TheArray[nIndex];
}
T LogicalSum() const
{
T sum = T();
for(int nIndex = 0; nIndex < SIZE; ++nIndex)
{
sum += TheArray[nIndex]; // Will require += to be available for target type T
}
return sum;
}
private:
T TheArray[SIZE];
};
int main(){
cout << "Start!" << endl;
Item<int> item1; // For class templates you have to explicitly pass template types.
item1.SetData(120);
item1.PrintData();
item1.PrintDataOnlyWorksForInt();
Item<float> item2;
item2.SetData(120.0);
item2.PrintData();
//item2.PrintDataOnlyWorksForInt(); // Will cause error during phase-two compilation when the code actually gets fully-compiled against the type for which it is being instantiated.
Array<int, 10> testArray;
cout << "End!" << endl;
return 0;
}