以前知道自己C++基础不行,看了C++ Primer,才真正慢慢开始了解C++。今天写了个int型的简单数组。
以后的学习的东西会慢慢写出来。
先是IntArray类()没有数组越界检查
IntArray.h文件
#ifndef IntArray_H
#define
IntArray_H
//
#include <iostream>
//
using namespace std;
class
IntArray

...
{
public:
//构造函数
IntArray(int sz = DefaultSize);
IntArray(int* pa,int size);
IntArray(const IntArray& par);
//析构函数
virtual ~IntArray();
//相等与不相等操作
bool operator==(const IntArray& par) const;
bool operator!=(const IntArray& par) const;

//赋值操作
IntArray& operator=(const IntArray& par);

//取数组大小

int size()...{return _size;}
//访问特定数组
virtual int find(int value);
//重载[]符号

virtual int& operator[](int index)...{return pArray[index];}
//void aaaaa(){cout<<"IntArray::aaaaa()!";}
protected:
//初始化数据函数
void Initial(int* pa,int size);
int _size;
int* pArray;
static const int DefaultSize;
}
;

#endif
IntArray.cpp文件
#include
"
IntArray.h
"

const
int
IntArray::DefaultSize
=
12
;

//
初始化函数
void
IntArray::Initial(
int
*
pa,
int
size)

...
{
//assert(size>0&&pa);
//申请空间
_size = size;
pArray = new int[_size];
//拷贝数据
for(int i = 0; i<_size;++i)

...{
if(!pa)

...{
pArray[i]=0;
}
else

...{
pArray[i] = pa[i];
}
}
}
//
构造函数
IntArray::IntArray(
int
sz)

...
{
Initial(0,sz);
}

IntArray::IntArray(
int
*
pa,
int
size)

...
{
Initial(pa,size);
}

//
析构函数
IntArray::
~
IntArray()

...
{
delete[] pArray;
}

IntArray::IntArray(
const
IntArray
&
par)

...
{
Initial(par.pArray,par._size);
}

//
查找特定数值
int
IntArray::find(
int
value)

...
{
//assert(index>0);
for(int i=0;i<_size;++i)

...{
if(pArray[i]==value)

...{
return i;
}
}
return -1;
}
//
重载==
bool
IntArray::
operator
==
(
const
IntArray
&
par)
const

...
{
if(_size!=par._size)

...{
return false;
}
for(int i=0;i<_size;++i)

...{
if(pArray[i]!=par.pArray[i])

...{
return false;
}
}
return true;
}
//
重载!=
bool
IntArray::
operator
!=
(
const
IntArray
&
par)
const
//
??常成员函数可以调用非常变量

...
{
if(_size!=par._size)

...{
return true;
}
for(int i=0;i<_size;++i)

...{
if(pArray[i]!=par.pArray[i])

...{
return true;
}
}
return false;
}

//
重载=
IntArray
&
IntArray::
operator
=
(
const
IntArray
&
par)

...
{
if(this != &par)

...{
//先释放资源
IntArray::~IntArray();
Initial(par.pArray,par._size);
}
return *this;
}
以下是个IntArratRC类,继承了IntArray类,增加了数组越界检查
IntArratRC.h文件
#ifndef IntArrayRC_H
#define
IntArrayRC_H
//
#include <iostream>
#include
"
IntArray.h
"
//
using namespace std;
class
IntArrayRC:
public
IntArray

...
{
public:
//构造函数
IntArrayRC(int sz = DefaultSize);
IntArrayRC(int* pa,int size);
IntArrayRC(const IntArrayRC& par);
virtual int& operator[](int index);

private:
void check_range(int index);
}
;
#endif
IntArratRC.cpp文件
#include
"
IntArrayRC.h
"
#include
<
cassert
>
//
构造函数
IntArrayRC::IntArrayRC(
int
sz):IntArray(sz)

...
{
//Initial(0,sz);
}
IntArrayRC::IntArrayRC(
int
*
pa,
int
size):IntArray(pa,size)

...
{
//Initial(pa,size);
}

IntArrayRC::IntArrayRC(
const
IntArrayRC
&
par):IntArray(par.pArray,par._size)

...
{
//Initial(par.pArray,par._size);
}

//
重写[]
inline
int
&
IntArrayRC::
operator
[](
int
index)

...
{
check_range(index);//越界检查
return pArray[index];
}

//
越界检查函数
inline
void
IntArrayRC::check_range(
int
index)

...
{
assert(index>=0&&index<_size);
}
写个函数测试一下:
#include
<
iostream
>
#include
"
IntArray.h
"
#include
"
IntArrayRC.h
"
#include
<
string
>

using
namespace
std;

int
main()

...
{

int ddm[5] = ...{1,2,3,4,5};

int eem[5] = ...{5,4,3,2,1};
//IntArray类
IntArray d;
IntArray a(ddm,5);
IntArray b(eem,5);
IntArray c(a);
cout<<"IntArray clsaa: ";
cout<<"a.size:"<<a.size()<<endl;
cout<<"a[4]="<<a[4]<<" ";
d=c;//赋值
if(a!=b)

...{
cout<<"a!=b"<<" ";
}
else

...{
cout<<"a==b"<<" ";
}

if(c==d)

...{
cout<<"c==d"<<" ";
}
else

...{
cout<<"c!=d"<<" ";
}

cout<<"c.size:"<<c.size()<<endl;
cout<<"c[4]="<<c[4]<<" ";

cout<<"d.size:"<<d.size()<<endl;
cout<<"d[5]="<<d[5]<<" ";//无越界检查

//IntArrayRC类
IntArrayRC dd;
IntArrayRC aa(ddm,5);
IntArrayRC bb(eem,5);
IntArrayRC cc(aa);
cout<<"IntArrayRC clsaa: ";
cout<<"aa.size:"<<aa.size()<<endl;
cout<<"aa[4]="<<aa[4]<<" ";
dd=cc;
if(aa!=bb)

...{
cout<<"aa!=bb"<<" ";
}
else

...{
cout<<"aa==bb"<<" ";
}
if(c==d)

...{
cout<<"cc==dd"<<" ";
}
else

...{
cout<<"cc!=dd"<<" ";
}
cout<<"cc.size:"<<cc.size()<<endl;
cout<<"cc[4]="<<cc[4]<<" ";
cout<<"dd.size:"<<dd.size()<<endl;
cout<<"dd[5]="<<dd[5]<<" ";//越界了

return 0;
}
