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
- Array类及其重载的运算符
- 重载运算符,Array类
- 重学C++ 带重载运算符的Array
- 十九、运算符重载(三)String类的改进实现、[]运算符重载、+运算符重载、+=运算符重载、<<运算符重载、>>运算符重载
- 类的运算符重载
- 运算符重载--类的赋值运算符重载
- C++运算符及其重载问题
- 运算符的重载:[],(),->;
- 运算符的重载
- 运算符的重载
- 运算符的重载
- 运算符的重载
- “[]”运算符的重载
- 运算符的重载
- 运算符的重载
- 运算符的重载
- 运算符的重载
- 重载的<<运算符
- gets strtok sscanf 实现读入一行并分割字符
- Tower of Cubes - UVa 10051 dp
- Ubuntu配置PPTP的VPN
- Hdu4155The Game of 31(DFS+博弈论)
- 韩语学习之第六课
- Array类及其重载的运算符
- 准备工作-用python绘制金融数据曲线的进阶实例
- git常用命令
- 突破IIS6上传文件尺寸限制
- [DFS序列+线段树维护区间标记]ZOJ3686 A Simple Tree Problem
- Intel 的AVX2指令集解读
- 和为S的两个数字
- Windows Phone中http请求收到The remote server returned an error: NotFound.
- 从一道趣味题目学习正则表达式