lecture9 notes

Inc endl cout ccontuppercase endl return 0 this

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: uppercase() { return toupper(elt); } }; int main() { Container<int> icont(5); Container<char> ccont('r'); cout << icont.inc() << endl; cout << ccont.uppercase() << endl; return 0; } This program prints out 6 and R on separate lines. Here, the class Container is given two implementations: a generic one and one specifically tailored to the char type. Notice the syntax at lines 14 and 15 when declaring a specialization. Finally, it is possible to parametrize templates on regular types: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <iostream> using namespace std; template <typename T, int N> class ArrayContainer { private: T elts[N]; public: T set(const int i, const T val) { elts[i] = val; } T get(const int i) { return elts[i]; } }; int main() { ArrayContainer <int, 5> intac; ArrayContainer <float, 10> floatac; intac.set(2, 3); floatac.set(3, 3.5); cout << intac.get(2) << endl; cout << floatac.get(3) << endl; 20 return 0; 21 } This program prints out...
View Full Document

Ask a homework question - tutors are online