Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types Require one or more type parameters to specify how to customize a generic class
Define template class template class ClassName { public: // constructor void set( T a); T get(); // other functions; private: T x; };
Implementation template Before each function definition template ClassName ::ClassName(){} template ClassName ::set( X a) { x = a;} template Y ClassName ::get(){ return x;}
Use template – main function When create a object of the template class, specify the data type be used for this object in a angle bracket Declare a variable ClassName x; ClassName y;