C++ 模板类
定义模板类
#ifndef PRO1_STACKTP_H#define PRO1_STACKTP_Htemplate <class Type>class Stack {private:    enum {MAX = 10};    Type items[MAX];    int top;public:    Stack();    bool isempty();    bool isfull();    bool push(const Type &item);    bool pop(Type &item);};template <class Type>Stack<Type>::Stack() {    top = 0;}template <class Type>bool Stack<Type>::isempty() {    return top == 0;}template <class Type>bool Stack<Type>::isfull() {    return top == MAX;}template <class Type>bool Stack<Type>::push(const Type &item) {    if (top < MAX) {        items[top++] = item;        return true;    } else        return false;}template <class Type>bool Stack<Type>::pop(Type &item) {    if (top > 0) {        item = items[--top];        return true;    } else        return false;}#endif //PRO1_STACKTP_H
#include "stacktp.h"
使用模板类
#include <iostream>#include <string>#include <cstring>#include <cctype>#include "module11_class_template/stacktp.h"using namespace std;// 使用模板类void useTemplateClass(){    Stack<string> st;    char ch;    string po;    cout << "Please enter A to add a purchase order, \n" << "P to process a PO, or to quit.\n";    while (cin >> ch && toupper(ch) != 'Q') {        while (cin.get() != '\n') {            continue;        }        if (!isalpha(ch)) {            cout << '\a';            continue;        }        switch (ch){            case 'A':            case 'a': cout << "Enter a PO number to add: ";            cin >> po;            if (st.isfull())                cout << "Stack already full\n";            else                st.push(po);            break;        case 'P':        case 'p': if (st.isempty())            cout << "Stack already empty\n";            else{                st.pop(po);                cout << "PO #" << po << " popped\n";                break;            }        }        cout << "Please enter A to add a purchase order ,\n" << "P to process a PO , or Q to quit.\n";    }    cout << "Bye\n";}int main() {    useTemplateClass();    return 0;}
模板类的非模板友元函数
#include <iostream>using namespace std;template <typename T>class HasFriend{private:    T item;    static int ct;public:    HasFriend(const T & i):item(i){ ct++; }    ~HasFriend();    friend void counts();    friend void reports(HasFriend<T> &);};// each specialization has it's own static data membertemplate <typename T>int HasFriend<T>::ct = 0;// non-template friend to all HasFriend<T> classesvoid counts(){    cout << "int count: " << HasFriend<int >::ct << "; ";    cout << "double count: " << HasFriend<double >::ct << endl;}// non-template friend to the HasFriend<int> classvoid reports(HasFriend<int> & hf){    cout << "HasFriend<int>: " << hf.item << endl;}// non-template friend to the HasFriend<double> classvoid reports(HasFriend<double> & hf){    cout << "HasFriend<double>: " << hf.item << endl;}void test(){    cout << "No objects declared: ";    counts();    HasFriend<int > hfil(10);    cout << "After hfil declared: ";    counts();    HasFriend<int > hfi2(20);    cout << "After hfi2 declared: ";    counts();    HasFriend<double > hfdb(10.5);    cout << "After hfdb declared: ";    counts();    reports(hfil);    reports(hfi2);    reports(hfdb);}
模板类的约束模板友元函数
#include <iostream>using namespace std;template <typename T> void counts();template <typename T> void report(T &);template <typename TT>class HasFriendT{private:    TT item;    static int ct;public:    HasFriendT(const TT & i) : item(i){ ct++; }    ~HasFriendT();    friend void counts<TT>();    friend void report<>(HasFriendT<TT> &);};template<typename T>int HasFriendT<T>::ct = 0;// template friend functions definitionstemplate <typename T>void counts(){    cout << "template size: " << sizeof(HasFriendT<T>) << "; ";    cout << "template counts()" << HasFriendT<T>::ct << endl;}template <typename T>void report(T & hf){    cout << hf.item << endl;}void test(){    counts<int>();    HasFriendT<int> hfil(10);    HasFriendT<int> hfi2(20);    HasFriendT<double > hfdb(10.5);    report(hfil);    report(hfi2);    report(hfdb);    cout << "counts<int>() output:\n";    counts<int>();    cout << "counts<double >() output:\n";    counts<double >();}
模板类的非约束模板友元函数
#include <iostream>using namespace std;template <typename T>class ManyFriend{private:    T item;public:    ManyFriend(const T & i) : item(i) {}    template <typename C, typename D> friend void show2(C &, D &);};template <typename C, typename D> void show2(C & c, D & d){    cout << c.item << ". " << d.item << endl;}void run(){    ManyFriend<int> hfi1(10);    ManyFriend<int> hfi2(20);    ManyFriend<double > hfdb(10.5);    cout << "hfi1, hfi2: ";    show2(hfi1, hfi2);    cout << "hfdb, hfi2: ";    show2(hfdb, hfi2);}