www.pudn.com > NZZHongjieF.rar > ARRAY.h, change:2009-04-26,size:2452b


#include<iostream> 
using namespace std; 
template <typename T> 
class Array 
{ 
private: 
	T* elements; 
	int size; 
public: 
  // explicit Array(intarraySize); 
	int Size(){return size;} 
	Array(){size=0;elements=NULL;} 
   Array(int arraySize); 
   Array(const Array &theArray); 
   ~Array(); 
   Array operator =(const Array &theArray); 
   bool operator ==(Array & test); 
   Array Add (T element); 
   T& operator[](int index); 
   const T&operator [](int index)const; 
   int Have(T value); 
   void Show(); 
   void Clear(); 
    
}; 
template<typename T> 
bool Array<T>::operator==(Array & test) 
{ 
	if(size!=test.Size()) 
		return false; 
	else 
		for(int i=0;i<size;i++) 
			if(elements[i]!=test[i]) 
				return false; 
			 return true; 
} 
template<typename T> 
void Array<T>::Clear () 
{ 
	size=0; 
 delete []elements; 
 elements=NULL; 
} 
template <typename T> 
void Array<T>::Show() 
{ 
 for(int i=0;i<size;i++) 
	 cout<<elements[i]; 
 cout<<endl; 
} 
template <typename T> 
int Array<T>::Have(T value) 
{ 
 for(int i=0;i<size;i++) 
 { 
	 if(elements[i]==value) 
		 return i+1; 
 } 
	 
		 return 0; 
} 
template <typename T> 
Array<T>:: Array(int arraySize){ 
  size=arraySize; 
  elements=new T[size]; 
} 
template<typename T> 
Array<T> Array<T>::Add(T element) 
{ 
	Array<T> temp=*this; 
 size=size+1; 
 if(elements!=NULL) 
 delete [] elements; 
 elements=new T[size+1]; 
 for(int i=0;i<size-1;i++) 
	elements[i]=temp.elements[i]; 
    elements[size-1]=element; 
	return *this; 
} 
template<typename T> 
Array<T>::Array(const Array& theArray) 
{ 
 size=theArray.size; 
 elements=new T[size]; 
 for(int i=0;i<size;i++) 
	elements[i]=theArray.elements[i]; 
} 
template<typename T> 
Array<T>::~Array() 
{ 
 delete []elements; 
} 
template <typename T> 
Array<T>  Array<T>::operator=(const Array &theArray) 
{ 
 if(this==&theArray) 
	 return *this; 
 else 
 { 
	 delete[]elements; 
	 size=theArray.size; 
	 elements=new T[size]; 
	 for(int i=0;i<size;i++) 
		 elements[i]=theArray.elements[i]; 
	 return * this; 
 } 
 
} 
 
template <typename T> 
T& Array<T>::operator[](int index) 
{ 
 if(index<0||index>=size) 
 { 
	 cout<<(index<0?"\nNegative index":"\n index too large")<<endl; 
	 exit(0); 
 } 
 else 
	 return elements[index]; 
} 
template <typename T> 
const T& Array<T>::operator [](int index) const 
{ 
if(index<0||index>=size) 
	 cout<<(index<0?"\nNegative index":"\n index too large")<<endl; 
 else 
	 return elements[index]; 
}