Array类及其重载的运算符

来源:互联网 发布:网络分配器 编辑:程序博客网 时间:2024/06/05 08:28

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为truebool 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
原创粉丝点击