Array.h
#pragma once
//
// fig10_09.hpp
// c++study
//
// Created by pop on 2017/6/8.
// Copyright © 2017年 pop. All rights reserved.
//
#include<iostream>
class Array
{
friend std::ostream &operator<<(std::ostream &, const Array &);
friend std::istream &operator>>(std::istream &, Array &);
public:
explicit Array(int = 10);
Array(const Array &);
~Array();
size_t 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:
size_t size;
int *ptr;
};
Array.cpp
//
// Array.cpp
// c++study
//
// Created by pop on 2017/6/8.
// Copyright © 2017年 pop. All rights reserved.
//
#include<iostream>
#include<iomanip>
#include<stdexcept>
#include"Array.h"
using namespace std;
Array::Array(int arraySize)
:size(arraySize > 0 ? arraySize :
throw invalid_argument("Array size must be greater than 0")),
ptr(new int[size])
{
for (size_t i = 0; i < size; i++)
ptr[i] = 0;
}
Array::Array(const Array &arrayToCopy)
:size(arrayToCopy.size),
ptr(new int[size])
{
for (size_t i = 0; i < size; i++)
ptr[i] = arrayToCopy.ptr[i];
}
Array::~Array()
{
delete[] ptr;
}
size_t 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 (size_t i = 0; i < size; ++i)
ptr[i] = right.ptr[i];
}
return *this;
}
bool Array::operator==(const Array &right) const
{
if (size != right.size)
return false;
for (size_t 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)
throw out_of_range("Subscript out of range");
return ptr[subscript];
}
int Array::operator[](int subscript) const
{
if (subscript < 0 || subscript >= size)
throw out_of_range("subscript out of range");
return ptr[subscript];
}
istream &operator>>(istream &input, Array &a)
{
for (size_t i = 0; i <a.size; ++i)
input >> a.ptr[i];
return input;
}
ostream &operator<<(ostream &output, const Array &a)
{
for (size_t i = 0; i < a.size; ++i)
{
output << setw(12) << a.ptr[i];
if ((i + 1) % 4 == 0)
output << endl;
}
if (a.size % 4 != 0)
output << endl;
return output;
}
fig10_09.cpp
//
// fig10_09.cpp
// c++study
//
// Created by pop on 2017/6/8.
// Copyright © 2017年 pop. All rights reserved.
//
#include <iostream>
#include <stdexcept>
#include"Array.h"
using namespace std;
int main()
{
Array integers1(7);
Array integers2;
cout << "size of integers1 is "
<< integers1.getSize()
<< "\nArray after initialization:\n" << integers1;
cout << "\nSize of Array integers2 is "
<< integers2.getSize()
<< "\nArray after initialization:\n" << integers2;
cout << "\nEnter 17 integers:" << endl;
cin >> integers1 >> integers2;
cout << "\nAfter input, the Array contain:\n"
<< "integers1:\n" << integers1
<< "integers2:\n" << integers2;
cout << "\nEvaluating: integers1 != integers2" << endl;
if (integers1 != integers2)
cout << "integers1 and integers2 are not equal1" << endl;
Array integers3(integers1);
cout << "\nSize of Array integers3 is "
<< integers3.getSize()
<< "\nArray after initialization:\n" << integers3;
cout << "\nAssigning integers2 to integers1:" << endl;
integers1 = integers2;
cout << "integers1:\n" << integers1
<< "integers2:\n" << integers2;
cout << "\nEvaluation: integers2 == integers2" << endl;
if (integers1 == integers2)
cout << "integers1 and integers2 are equal" << endl;
cout << "\nintegers1[5] is " << integers1[5];
cout << "\n\nAssigning 1000 to integers1[5]" << endl;
integers1[5] = 1000;
cout << "integers1:\n" << integers1;
try
{
cout << "\nAttempt to assign 1000 to integers1[15]" << endl;
integers1[15] = 1000;
}
catch (out_of_range &ex)
{
cout << "An exception occurred: " << ex.what() << endl;
}
}