lecture9 notes

5 35 cout fpointgetx fpointgety endl return

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: T getY() { return y; } }; int main() { Point<float> fpoint(2.5, 3.5); cout << fpoint.getX() << ", " << fpoint.getY() << endl; return 0; } The program prints out 2.5, 3.5. To declare member functions externally, we use the following syntax: template <typename T> T classname<T>::function_name() So, for example, getX could have been declared in the following way: template <typename T> T Point<T>::getX() { return x; } assuming a prototype of T getX(); inside the class definition. We can also define different implementations for a single template by using template specialization. Consider the following example: 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 #include <iostream> #include <cctype> using namespace std; template <typename T> class Container { private: T elt; public: Container(const T arg) : elt(arg) {} T inc() { return elt+1; } }; template <> class Container <char> { private: char elt; public: Container(const char arg) : elt(arg) {} char...
View Full Document

Ask a homework question - tutors are online