设计一个double类型的数组类
发布网友
发布时间:2023-07-07 04:36
我来回答
共2个回答
热心网友
时间:2023-09-16 20:44
#include<iostream>
using namespace std;
class CDBLArray
{
private:
int size;
double *Array;
public:
CDBLArray(int s0)
{
size = s0;
}
CDBLArray(double a1[],int s1)
{
Array = new double[s1];
for(int i = 0; i <s1;i++)
Array[i] = a1[i];
size = s1;
}
~CDBLArray(){
cout<<"Invoking destructor,clearing up"<<endl;
if(Array != NULL) delete [] Array;
}
void Resize(int newsize)
{
double *Array1 = Array;
int oldsize =size;
Array = new double[newsize];
for(int i =0 ;i<oldsize ;i++)
Array[i] = Array1[i];
delete [] Array1;
size = newsize;
}
int Size()
{
return size;
}
CDBLArray& operator=(const CDBLArray& ary)
{
if(&ary != this){
Array = ary.Array;
size = ary.size;
}
return *this;
}
double&operator[](int i){
if(i >= size){
cout<<"访问越界"<<endl;
exit(-1);
}
return Array[i];
}
};
void main()
{
double a[8] = {1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8};
CDBLArray ary1(5),ary2(a,5);
CDBLArray ary3 = ary2;
ary2.Resize(8);
cout <<ary2.Size()<<endl;
int i;
for(i = 5;i < 8;i++)
ary2[i] = a[i];
for(i = 0;i < 8;i++)
cout <<ary2[i]<<" ";
cout <<endl;
ary3 = ary2;
for(i = 0;i < 8;i++)
cout <<ary3[i]<<" ";
cout <<endl;
cout<<ary3[8]<<endl;
}
热心网友
时间:2023-09-16 20:44
设计一个double类型的数组类CDBLArray,要求CDBLArray可以原创的文章,质量的保证。