fig10_09.cpp

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;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值