Array类及其重载的运算符

Array.h

#ifndef ARRAY_H
#define ARRAY_H
#include<iostream>
using namespace std;
class Array
{
<span style="white-space:pre">	</span>friend ostream &operator<<(ostream &,const Array &);
<span style="white-space:pre">	</span>friend istream &operator>>(istream &,Array &);
<span style="white-space:pre">	</span>public:
	<span style="white-space:pre">	</span>Array(int=10);
	<span style="white-space:pre">	</span>Array(const Array &);
	<span style="white-space:pre">	</span>~Array();
	<span style="white-space:pre">	</span>int getSize() const;
	<span style="white-space:pre">	</span>const Array &operator=(const Array&);
	<span style="white-space:pre">	</span>bool operator==(const Array &) const;
	<span style="white-space:pre">	</span>bool operator!=(const Array &right)const;
	<span style="white-space:pre">	</span>{return !(*this==right);}
	<span style="white-space:pre">	</span>int operator[](int);
 <span style="white-space:pre">	</span>private:
	<span style="white-space:pre">	</span>int size;
	<span style="white-space:pre">	</span>int *ptr;
};

Array.cpp

#include<iostream>
using namespace std;
#include<iomanip>
#include<cstdlib>
#include"Array.h"
Array::Array(int arraySize)
{size=(arraySize>0?arraySize:10);
 ptr=new int[size];
 for(int i=0;i<sze;i++)
    ptr[i]=0;
 }
Array::Array(const Array &arrayTocopy)
   :size(arrayTocopy.size)
{ptr=new int[size];
 for(int i=0;i<size;i++)
   ptr[i]=arrayTocopy.ptr[i];
}
Array::~Array()
{delete [] ptr;
}
int Array::getSize() const
{return size;
}
const Array &Array::operator=(const Array &right)
{if(&right!=this)
   {if(size!=right.size)
	{delete [] ptr;
	 size=right.size;
	 ptr=new int[size];
	 }
    for(int i=0;i<size;i++)
        ptr[i]=right.ptr[i];
    }
 return *this;
}
//bool类型在每一种语言中都是有相同的含义,即真那就是true,是假就//为false,但在不同的语言中判断一个布尔值是否为真有所不同。C++中//如果值非零就为True,为零就是False。比如:
//bool   b;
//b=(1>2)  //此时b为false
//b=(2>1)  //此时b为true

bool Array::operator==(const Array &right) const
{if(size!=right.size)
	return false;
 for(int i=0;i<size;i++)
     if(ptr[i]=right.ptr[i])
  	return false;
 return true;
}
int &Array::operator[](int subscript)
{if(subscript<0||subscript>=size)
  { cerr<<"/nError:subscript"<<subscript<<"out of range"<<endl;
    exit(1);
   }
 return ptr[subscript];
}
int Array::operator[](int subscript) const
{if(subscript<0||subscript>=size)
  { cerr<<"/nError:subscript"<<subscript<<"out of range"<<endl;
    exit(1);
   }
 return ptr[subscript];
}
istream &operator>>(istream &input,Array &a)
{for(int i=0;i<a.size;i++)
    input>>a.ptr[i];
 return input;
}
ostream &operator<<(ostream &output,const Array  &a)
{int i;
 for(i=0;i<a.size;i++)
  {output<<setw(12)<<a.ptr[i];
   if((i+1)%4==0)
	output<<endl;
   }
 if(i%4!=0)
    ouput<<endl;
 return output;
}

demo.cpp

#include<iostream>
using namespace std;
#include<Array.cpp>
int main()
{Array integers1(7);
 Array integers2;
 cout<<integers1.getSize()<<integers1;
 cout<<integers2.getSize()<<integers2;
 cout<<"enter 17 integers"<<endl;
 cin>>integers1>>integers2;
 cout<<integers1<<integeers2;
 if(integers1!=integers2)
    cout<<"not equal"<<endl;
 Array integers3(integers1);
 cout<<integers3.getSize()<<integers3;
 integers1=integers2;
 cout<<integers1<<integers2;
 if(integers1==integers2)
    cout<<" equal"<<endl;
 cout<<integers1[5];
 integers1[5]=1000;
 cout<<integers1;
 integers1[15]=1000;
 return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值