自己写的Array类

 

这是今天自己写的一个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& outconst 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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值