输出:
y a n g z h o u 3 0 1 1 9 2 6 0 8 1 7 array full
代码如下:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
template<typename T>
class Array{
public:
Array(int length){
ch = new T[length];
count = 0;
this->length = length;
}
~Array(){
delete[] ch;
}
void insert(char c){
if (count >= length){
cout << "array full" << endl;
}else{
ch[count] = c;
count += 1;
}
}
void showAll(){
for(int i = 0; i < length; i++){
cout << ch[i] << endl;
}
}
private:
T* ch;
int count;
int length;
};
int main() {
string str1="yangzhou301";
Array<char> arr1(str1.length());
for(auto c:str1)
{
arr1.insert(c);
}
arr1.showAll();
int num[]={1,9,2,6,0,8,1,7};
Array<int>arr2(sizeof(num)/sizeof(int));
for(auto c:num)
{
arr2.insert(c);
}
arr2.showAll();
arr2.insert(301);
return 0;
}