这是今天自己写的一个array类,可能还存在问题,希望大家指正!
由于vc6编译器不支持 定义跟实现分别编译,否则出现link2001的错误。
用的时候,#include"array.cpp"即可
//array.h
/**//********************************************************************
自定义array类
1. 判断下标是否越界
2. 用==, != 来判断两个数组是否相等
3.一次输入/输出 整个数组
4.用=运算符把一个数组赋值个另一个数组
*********************************************************************/
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
using namespace std;
template<class Type>
class Array...{
friend ostream& operator<<(ostream& out, const Array<Type>& a);
friend istream& operator>>(istream& input, Array<Type>& a);
private:
Type *data; //array
int size; //length of array
public:
Array(int sz = 10); //default constructor
Array(const Type *x, int length); //copy constructor
Array(const Array<Type>& x); //copy constructor
~Array(); //destructor
int getLength() const; //get length of this array
Array<Type>& operator=(const Array<Type>& a); //copy: ar1 = ar2;
Type& operator[](int i); //array[i] = n;
const Type& operator[](int i) const; //int n = array[i];
bool operator==(const Array<Type>& a);
bool operator!=(const Array<Type>& a);
};
#endif
//array.cpp
#include "array.h"
#include <cassert>
#include <iomanip>
template<class Type>
ostream& operator<<(ostream& out, const Array<Type>& a)...{
for(int i = 0; i < a.size; i++)
out << setw(4) << a.data[i];
return out;
}
template<class Type>
istream& operator>>(istream& input, Array<Type>& a)...{
for(int i = 0; (i < a.size); i++)...{
input >> a.data[i];
}
return input;
}
template<class Type>
Array<Type>::Array(int sz)...{
size = (sz > 0 ? sz : 10); //validate size
data = new Type[size];
assert(data);
//initialize array
for(int i = 0; i < size; i++)
data[i] = 0;
}
template<class Type>
Array<Type>::Array(const Type *x, int length)...{
size = length;
data = new Type[length];
assert(data);
for(int i = 0; i < size; i++)
data[i] = x[i];
}
template<class Type>
Array<Type>::Array(const Array<Type>& x)...{
size = x.size;
data = new Type[size]; //create space for arrya
assert(data);
for(int i = 0; i < size; i++)//copy one by one
data[i] = x.data[i];
}
template<class Type>
Array<Type>::~Array()...{
delete[] data;
}
template<class Type>
int Array<Type>::getLength() const...{
return size;
}
template<class Type>
Array<Type>& Array<Type>::operator=(const Array<Type>& a)...{
if(*this != a)...{ //self-copy
if(size != a.size)...{ // different size
delete[] data;
size = a.size;
data = new Type[size];
assert(data);
}
for(int i = 0; i < size; i++)
data[i] = a.data[i];
}
return *this;
}
template<class Type>
const Type& Array<Type>::operator[](int i) const...{
assert(i>=0 && i <size);
return data[i];
}
template<class Type>
Type& Array<Type>::operator[](int i)...{
assert(i>=0 && i <size);
return data[i];
}
template<class Type>
bool Array<Type>::operator ==(const Array<Type>& a)...{
if(size != a.size) //judge size
return false;
for(int i = 0; i < size; i++) //judge element
if(data[i] != a.data[i])
return false;
return true;
}
template<class Type>
bool Array<Type>::operator !=(const Array<Type>& a)...{
return !(*this == a);
}
1484

被折叠的 条评论
为什么被折叠?



