追寻梦想的路

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Array类定义

 

#include <iostream>
using namespace std;

template <class elemType>
class Array
{
public:
    Array(){
        cout << "调用构造函数" << endl;
    };

    Array(elemType *temp, int n){        
        m_size = n;
        a = new elemType[m_size];
        for (int i = 0; i < m_size; i++)
        {
            a[i] = temp[i];
        }
        cout << "调用构造函数" << endl;
    };

    void Print(void)
    {
        for (int i = 0; i < this->m_size-1; i++)
        {
            cout << this->a[i] << " ";
        }
        cout << this->a[m_size-1] << endl;
    }


    ~Array(){
        cout << "调用析构函数" << endl;
    };

private:
    int m_size;
    elemType *a;  

};

int main()
{
    int temp1[6] = { 1, 2, 3, 4, 5, 6 };
    float temp2[10] = { 1.2, 2.2, 3.2, 4.2, 5.2, 6.2 };
    Array <int> a(temp1,6);
    Array <float> b(temp2, 6);
    a.Print();
    b.Print();
    //Array <float> b(temp2,6);
    
    //cout << "hello" << " world" << endl;
    return 0;
}

 

posted on 2015-10-19 13:26  追寻梦想的路  阅读(573)  评论(0编辑  收藏  举报