// Template <template parameter table> // class declaration // defines a class template. The format of a class template is similar to that of a function template. The template must start with the keyword, the following is the template parameter enclosed in angle brackets, followed by the class name. The format is as follows: // template <class type> // class name {////... //}; // Where template is the keyword of a declaration template, it indicates declaring a template. class indicates that the following type is a class template parameter // in the class definition, the parameters or return values of the data members and member functions of the general data type are to be used, when you need to add type // to define in vitro member functions for a class, if a template parameter exists in this member function, you need to declare the template in vitro, in addition, the class name suffix before the function name is "<type>" // For example, a member function is defined: // template <class T> // T complex <t>: realcomplex () // {return real;} // class modulo A board does not represent a specific and actual class, but a class. In fact, a class template is used to instantiate a class template into a specific class in the format of: // class name <actual type> Object Name; // For example, use the preceding class template to create two double-type objects. The statement is as follows: // complex <double> S1, S2; // create two int-type objects: // complex <int> S1, S2; # include <iostream. h> # include <stdlib. h> // struct studentstruct student {int ID; // student ID float GPa; // average score}; // class template: template <class T> class store {PRIVATE: T item; // used to store any type of data int havevalue; // It is used to mark whether the item has been stored in the public: store (void); // The default form (invisible parameter) of the constructor t getelem (void ); // extract the data function void putelem (t x); // store the data function}; // The implementation template of the default constructor <class T> store <t> :: store (void): havevalue (0) {}template <class T> // data extraction function implementation t store <t >:: getelem (void) {// if you try to extract uninitialized data, terminateProgram If (havevalue = 0) {cout <"no item present! "<Endl; exit (1);} return item; // return the data stored in the item} template <class T> // implement void store for data storage function <t>: putelem (t x) {havevalue ++; // set havevalue to true, which indicates that the value item = X has been saved to the item; // The value x is saved to the item} int main (void) {student G = {1000, 23}; store <int> S1, S2; store <student> S3; store <double> D; s1.putelem (3); s2.putelem (-7); cout <s1.getelem () <"" <s2.getelem () <Endl; s3.putelem (g); cout <"the student ID is" <s3.getelem (). ID <Endl; cout <"retrieving Object D"; cout <D. getelem () <Endl; // data member of the output object d Return 0 ;}