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
|
// program testing
#include <iostream>
using namespace std;
// template class Container to contain array of type T with array size N
template <typename T, int N>
class Container {
private:
T v[N];
public:
void setMember(int pos, T value);
T getMember(int n);
};
template <typename T, int N>
void Container<T, N>::setMember(int pos, T value) {
v[pos] = value;
}
template <typename T, int N>
T Container<T, N>::getMember(int pos) {
return v[pos];
}
// specialized template class Container to contain array of type char with array size N
template <char, int N>
class Container<char, int N> {
private:
char v[N];
public:
void setMember(int pos, char c);
char getMember(int pos);
char getUpperMember(int pos);
};
template <char, int N>
void Container<char, int N>::setMember(int pos, char c) {
v[pos] = value;
}
template <char, int N>
char Container<char, int N>::getMember(int pos) {
return v[pos];
}
template <char, int N>
char Container<char, int N>::getUpperMember(int pos) {
return (v[pos] >= 'a' && v[pos] <= 'z')? v[pos] - ('a' - 'A'): v[pos];
}
// mains
int main() {
Container<int, 10> container1;
container1.setMember(5, 50);
cout << container1.getMember(5) << endl;
return 0;
}
|