CMPT 212
Fall 1997
|
|
void swap(int & x, int & y) { int temp = x; x = y; y = temp; }
template <class T> void swap(T & x, T & y) { T temp = x; x = y; y = temp; }
int main() { int a = 3; int b = 7; float f = 3.14; float g = 2.32; myObject r; myObject s; swap(a, b); swap(f, g); swap(r, s); // provided that "myObject" class // has an assignment operator. return 0; }
class elem { public: elem * next; person p; }; class list { public: void addToEnd(person & p); person * getFirst(); person * getNext(); ... private: elem * head; elem * curr; };
template <class T> class elem { public: elem * next; T p; }; template <class T> class list { public: void addToEnd(T & p); T * getFirst(); T * getNext(); ... private: elem<T> * head; elem<T> * curr; };
int main() { list<person> list1; list<elephant> list2; list<char> list3; person p; elephant e; ... list1.addToEnd(p); list2.addToEnd(e); list3.addToEnd('w'); ... return 0; }
template<class K, class V> class association { K key; V value; }; association<int, person> mytable[100];
template<class K, class V, int size=100> class lookup { public: association<K, V> data[size]; }; lookup<int, person, 100> mytable;
Return to lecture notes index |
|
This page is maintained by simpson@cs.sfu.ca. | Last updated on 28 Nov 1997. |