重载运算符,Array类

/*main.h*/
#include
#include
#include
using namespace std;

class Array
{
    friend ostream &operator<<(ostream &,const Array &);
    friend istream &operator>>(istream &,Array &);
public:
    Array (int =10);
    Array (const Array &);
    ~Array();
    int getSize() const;

    const Array &operator=(const Array &);
    bool operator==(const Array &)const;
    bool operator!=(const Array &right) const
    {
        return !(*this ==right);
    }

    int &operator[](int);

    int operator[](int) const;
private:
    int size;
    int *ptr;
};


#include
#include
#include
#include "main.h"

using namespace std;

Array::Array(int arraySize)
{
    size=(arraySize>0 ? arraySize :0);
    ptr=new int[size];
    for(int i=0;i     ptr[i]=0;
}

Array::Array(const Array &arrayToCopy)
  : size(arrayToCopy.size)
  {
      ptr=new int[size];

      for(int i=0;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             ptr[i]=right.ptr[i];
    }
    return *this;
}

bool Array::operator==(const Array &right) const
{
    if(size!=right.size)
    return false;

    for(int i=0;i     if(ptr[i]!=right.ptr[i])
        return false;
    return true;
}

int &Array::operator[](int subscript)
{
    if(subscript<0||subscript>0)
    {
        cerr<<"\nError:Subscript "<         exit(1);
    }
    return ptr[subscript];
}

int Array::operator[](int subscript) const
{
    if(subscript<0||subscript>0)
    {
        cerr<<"\nError:Subscript "<         exit(1);
    }
    return ptr[subscript];
}

istream &operator>>(istream &input,Array &a)
{
    for(int i=0;i         input >> a.ptr[i];
    return input;
}

ostream &operator<<(ostream &output,const Array &a)
{
    int i;
    for(i=0;i     {
        output<         if((i+1)%4==0)
            output<     }
    if(i%4!=0)
        output<     return output;
}


/*main*/
#include
#include "main.h"
using namespace std;

int main()
{
    Array integers1(7);
    Array integers2;
    cout <<"size of integers1 is : "<     <<"\nArray after initilization: \n"<     cout <<"size of integers2 is : "<     <<"\nArray after initilization: \n"<
    cout <<"\nEnter 17 integers:"<     cin >>integers1>>integers2;
    cout<<"\nAfter input,the Arrays contains: \n"
    <<"integers1:\n"<     <<"integers2:\n"<
    cout <<"\nEvaluating: integers1!=integers2"<     if(integers1!=integers2)
        cout <<"integers1 and integers2 are not equal"<
    Array integers3(integers1);
    cout <<"size of integers3 is : "<     <<"\nArray after initilization: \n"<
    cout <<"\nAssigning integers2 to integers1:"<     integers1=integers2;
    cout<<"integers1:\n"<     <<"integers2:\n"<
    cout <<"\nEvaluating: integers1==integers2"<     if(integers1==integers2)
        cout <<"integers1 and integers2 are equal"<
    cout <<"\nintegers1[5] is "<
    cout <<"\nAssigning 1000 to integers1[5]:"<     integers1[5]=1000;
    cout<<"integers1:\n"<
    cout <<"\nAttempt to assing 1000 to integers1[15] "<     integers1[15]=1000;
    return 0;
} 重载运算符,Array类_第1张图片

你可能感兴趣的:(重载运算符,Array类)