// 包含动态数组成员的类.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <iostream>
    #include <cstdlib>
    using namespace std;

    //此类的对象是一个部分填充的double类型的数组
    class PFArrayD
    {
    public:
       PFArrayD();
       PFArrayD(int capacityValue);
       PFArrayD(const PFArrayD& pfaObject);    //复制构造函数

       void addElement(double element);

       bool full() const {return (capacity == numberUsed) ;}

       int getCapacity() const {return capacity;}

       int getNumberUsed() const {return numberUsed;}

       //清空数组
       void emptyArray() {numberUsed = 0;}

       double& operator [](int index);

       PFArrayD& operator =(const PFArrayD& rightSide); //重载的赋值操作

       ~PFArrayD();
    private:
       double *a;
       int capacity;
       int numberUsed;
    };


    void testPFArrayD();

    int main(int argc, char* argv[])
    {
       cout <<"This program tests the class PFArrayD.\n";
 
       char ans = 'y';
       do
       {
          testPFArrayD();
          cout <<"Test again?(y/n)";
          cin >>ans;
       }while( (ans == 'Y') || (ans == 'y') );
 
       return 0;
    }

    PFArrayD::PFArrayD() : capacity(50), numberUsed(0)
    {
       a = new double[capacity];
    }

    PFArrayD::PFArrayD(int capacityValue) : capacity(capacityValue), numberUsed(0)
    {
       a = new double[capacity];
    }

    PFArrayD::PFArrayD(const PFArrayD& pfaObject)
      : capacity(pfaObject.getCapacity() ), numberUsed(pfaObject.getNumberUsed() )
    {
       a = new double[capacity];
       for(int i = 0; i < numberUsed; ++i)
          a[i] = pfaObject.a[i];
    }

    void PFArrayD::addElement(double element)
    {
       if(numberUsed >= capacity)
       {
          cout <<"Attempt to exceed capactiy in PFArrayD.\n";
          exit(0);      //<cstdlib>
       }

       a[numberUsed] = element;
       ++numberUsed;
    }

    double& PFArrayD::operator [](int index)
    {
       if(index >= numberUsed)
       {
          cout <<"Illegal index in PFArrayD.\n";
          exit(0);
       }

       return a[index];
    }

    PFArrayD& PFArrayD::operator =(const PFArrayD& rightSide)
    {
       if(capacity != rightSide.capacity)      //不但可保证赋值操作符左边的对象有正确大小的数组,
       {                        //还可保证若赋值操作符两边是同一个对象,则其中成员变量a所指向的数组不会被删除。
          delete[] a;
          a = new double[rightSide.capacity];
       }

       capacity = rightSide.capacity;
       numberUsed = rightSide.numberUsed;
       for(int i = 0; i < numberUsed; ++i)
          a[i] = rightSide.a[i];

       return *this;
   }

   PFArrayD::~PFArrayD()
   {
       delete[] a;
   }


   void testPFArrayD()
   {
       int cap = 0;
       cout <<"Enter capacity of this array:";
       cin >>cap;
       PFArrayD temp(cap);

       cout <<"Enter up to " << cap <<" nonnegative numbers.\n";
       cout <<"Place a negative number at the end.\n";

       double next = 0;
       cin >>next;
       while( (next >= 0) && !temp.full() )
       {
          temp.addElement(next);
          cin >>next;
       }

       cout <<"You entered the following "
           << temp.getNumberUsed() <<" numbers:\n";
       int index = 0;
       int count = temp.getNumberUsed();
       for(; index < count; ++index)
          cout <<temp[index]<< ' ';
       cout <<endl;
   }

 

   //复制构造函数、赋值操作符、析构函数可称作三合一

    

 

 

 

 posted on 2012-05-04 15:13  飞翔@骑士  阅读(150)  评论(0)    收藏  举报