pfarrayd.cpp
来自「AbsoluteC++中文第二版书上的源代码」· C++ 代码 · 共 72 行
CPP
72 行
//This is the IMPLEMENTATION FILE: pfarrayd.cpp.
//This is the IMPLEMENTATION of the class PFArrayD.
//The interface for the class PFArrayD is in the file pfarrayd.h.
#include "pfarrayd.h"
#include <iostream>
using std::cout;
PFArrayD::PFArrayD( ) :capacity(50), used(0)
{
a = new double[capacity];
}
PFArrayD::PFArrayD(int size) :capacity(size), used(0)
{
a = new double[capacity];
}
PFArrayD::PFArrayD(const PFArrayD& pfaObject)
:capacity(pfaObject.getCapacity( )), used(pfaObject.getNumberUsed( ))
{
a = new double[capacity];
for (int i =0; i < used; i++)
a[i] = pfaObject.a[i];
}
void PFArrayD::addElement(double element)
{
if (used >= capacity)
{
cout << "Attempt to exceed capacity in PFArrayD.\n";
exit(0);
}
a[used] = element;
used++;
}
double& PFArrayD::operator[](int index)
{
if (index >= used)
{
cout << "Illegal index in PFArrayD.\n";
exit(0);
}
return a[index];
}
PFArrayD& PFArrayD::operator =(const PFArrayD& rightSide)
{
if (capacity != rightSide.capacity)
{
delete [] a;
a = new double[rightSide.capacity];
}
capacity = rightSide.capacity;
used = rightSide.used;
for (int i = 0; i < used; i++)
a[i] = rightSide.a[i];
return *this;
}
PFArrayD::~PFArrayD( )
{
delete [] a;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?