C/C++_log2000_函数模板与类模板笔记2

来源:互联网 发布:java的形参和实参 编辑:程序博客网 时间:2024/04/28 23:47

函数模板与类模板的相关笔记 entry2

函数模板; 类模板;

类模板的一般定义形式为:

template <类型形参表> class 类名{    类声明体;}建立类模板之后,可以用类型实参定义模板类,并创建实例。模板类是一个实实在在的类,对象是该模板类的势力。其格式如下:类名 <类型实参表> 对象;

下面看几个例子
例一 :定义数组类模板

#include <iostream>using namespace std;template <class T>class Array{private:    T *arr;public:    Array(int c)    {         arr = new T[c];    }     void init(int i, T x)    {          arr[i] = x;    }     T& operator[](int i)    {          return arr[i];    }};void main(){    Array<int> array(5);    cout << "input every element's value: " << endl;    for (int i = 0; i < 5; i++){        cin >> array[i];    }    cout << "every element's value in Array is: " << endl;    for (i = 0; i < 5; i++){        cout << "No " << i << " : " << array[i] << endl;    }}-------------------------------------运行结果:input every element's value:6 21 27 17 40every element's value in Array is:No 0 : 6No 1 : 21No 2 : 27No 3 : 17No 4 : 40

例二 类模板参数是类

类模板的参数不仅可以是标准数据类型,还可以是用户自定义类型,当然包括用户自己定义的类,如下所示:

#include <iostream>using namespace std;class A{private:    int j;public:    A(){}    A(int x):j(x){}    A(A* x)    {        j = x->j;    }    void operator!()    {        cout << "j = " << j << endl;    }};template<typename T>class B{private:    int i;    T* x;public:    B(int xa, T* p): i(xa){        x = new T(p);    }    void operator!()    {        cout << "i = " << i << endl;        !*x;    }};void main(){    A a(1);    B<A> b(2, &a);    !b;}----------------------------------运行结果:i = 2j = 1

例三

#include <iostream>using namespace std;template<class T>class Stack{public:    Stack(int = 10);    //default constructor(stack size 10)     ~Stack(){        delete[]stackPtr;   //destructor    }    bool push(const T&);   //push an element onto the stack    bool pop(T&);                    //pop an element off the stackprivate:    int size;          //# of elements in the stack     int top;           //location of the top element    T* stackPtr;  //pointer to the stack    bool isEmpty() const{        return top == -1;    }    bool isFull() const{        return top == size-1;    }};//Constructor with default size 10template<class T>Stack<T>::Stack(int S){    size = S > 0 ? S : 10;    top = -1;                     //Stack is initially empty    stackPtr = new T[size];       //allocate space for elements}//Push an element onto the stack//return true if successful false otherwisetemplate <class T>bool Stack<T>::push(const T& pushValue){    if (!isFull()){        stackPtr[++top] = pushValue;           //place item in Stafck        return true;                           //push successful    }       return false;}//Pop an element off the stacktemplate<class T>bool Stack<T>::pop(T& popValue){    if (!isEmpty()){        popValue = stackPtr[top--];         //remove item from Stack        return true;                        //pop successful    }    return false;}void main(){    Stack<double> doubleStack(5);    double d = 1.1;    cout << "Pushing elements onto doubleStack\n";    while (doubleStack.push(d)){        //success true returned        cout << d << ' ';        d += 1.1;    }    cout<< "\nStack is full. cannot push " << d        << "\n\nPoping elements from doubleStack\n";     while (doubleStack.pop(d)){              //success true returned        cout << d << ' ';    }    cout << "\nStack is empty.Cannot pop\n";    Stack<int> intStack;    int i = 1;    cout << "\nPushing elements onto intStack\n";    while (intStack.push(i)){        //success true returned        cout << i << ' ';        ++i;    }    cout<< "\nStack is full.Cannot push " << i        << "\n\nPopping elements from intStack\n";    while (intStack.pop(i)){              //success true returned        cout << i << ' ';    }    cout << "\nStack is empty.Cannot pop\n";}--------------------------------------------------运行结果:Pushing elements onto doubleStack1.1 2.2 3.3 4.4 5.5Stack is full. cannot push 6.6Poping elements from doubleStack5.5 4.4 3.3 2.2 1.1Stack is empty.Cannot popPushing elements onto intStack1 2 3 4 5 6 7 8 9 10Stack is full.Cannot push 11Popping elements from intStack10 9 8 7 6 5 4 3 2 1Stack is empty.Cannot pop

visitor tracker
访客追踪插件


原创粉丝点击