array.h
array.cpp#ifndef __array_h__
#define __array_h__
#include using namespace std;
template class array
public:
array(int len = 0)//構造
this->len = len;
m_p = new t[len];
} array(const array &a)//拷貝構造
this->len = a.len;
m_p = new t[a.len];
for (int i = 0; i < len; i++)
m_p[i] = a.m_p[i];
} ~array()//析構
array& operator=(const array &a)//賦值過載
return *this;
} t& operator(int index)// 陣列下標過載
private:
int len;//陣列長
t *m_p;//陣列指標
};#endif //__array_h__
#include #include "array.h"
using namespace std;
int main1()
//類 的陣列
class student
student(int id, char *name)
private:
int id;
char *name;
};//過載輸出類的 《運算子
ostream& operator<< (ostream& out, student &s)
int main2()
; arrays(5);
//陣列中存的是值,往陣列裡存資料是值的複製
//會呼叫賦值運算子過載函式
for (int i = 0; i < 5; i++)
cout << s << endl;
return 0;
}int main()
C 模板 37 類模板練習 自定義陣列類
到這裡,模板相關基本學習完,現在來利用模板類寫乙個 例子。這個例子就是自定義乙個陣列模板類,需要實現陣列的一些常用操作。例如拷貝,有參構造,查詢,刪除等。1.自定義陣列類需求 模板案例描述 實現乙個通用的陣列類,要求如下 1 可以對內建資料型別以及自定義資料型別的資料進行儲存 2 將陣列中的資料儲存...
C 有界陣列模板類(類模板)
題目描述編寫有界陣列模板boundarray 即檢查對陣列元素下標引用並在下標越界時終止程式的執行 能夠儲存各種型別的資料。要求實現對陣列進行排序的方法sort,及對排序後的陣列進行查詢的方法search。輸入 第一行先輸入t,表示有t個測試用例 從第二行開始輸入每個測試用例的資料。首先輸入資料型別...
陣列類模板
問題及描述 檔名稱 test.cpp 作者 邱凱 完成日期 2016年5月31號 版本號 v6.0 問題描述 陣列類模板 輸入描述 輸入資料 輸出描述 輸出答案 include include include using namespace std template class array temp...