Advertisement
Guest User

C++ Class

a guest
Apr 3rd, 2020
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. include <iostream>
  2. using namespace std;
  3. class PFArrayD
  4. {
  5.     public:
  6.         PFArrayD();
  7.         PFArrayD(int capacityValue);
  8.         PFArrayD(const PFArrayD& pfaObject);
  9.         void addElement(double element);
  10.         bool full() const { return (capacity == used); }
  11.         int getCapacity() const { return capacity; }
  12.         int getNumberUsed() const { return used; }
  13.         void emptyArray() { used = 0; }
  14.         double& operator[](int index);
  15.         PFArrayD& operator =(const PFArrayD& rightSide);
  16.         ~PFArrayD();
  17.     private:
  18.         double *a;
  19.         int capacity;
  20.         int used;
  21. };
  22. PFArrayD::PFArrayD() :capacity(50), used(0){
  23.     a = new double[capacity];
  24. }
  25. PFArrayD::PFArrayD(int size) : capacity(size), used(0){
  26.     a = new double[capacity];
  27. }
  28. PFArrayD::PFArrayD(const PFArrayD& pfaObject): capacity(pfaObject.getCapacity()), used(pfaObject.getNumberUsed()){
  29.     a = new double[capacity];
  30.     for (int i = 0; i < used; i++)
  31.     a[i] = pfaObject.a[i];
  32. }
  33. void PFArrayD::addElement(double element)
  34. {
  35.     if (used >= capacity){
  36.         cout << "Attempt to exceed capacity in PFArrayD.\n";
  37.         exit(0);
  38.     }
  39.     a[used] = element;
  40.     used++;
  41. }
  42. double& PFArrayD::operator[](int index){
  43.     if (index >= used){
  44.         cout << "Illegal index in PFArrayD.\n";
  45.         exit(0);
  46.     }
  47.     return a[index];
  48. }
  49. PFArrayD& PFArrayD::operator =(const PFArrayD& rightSide){
  50.     if (capacity != rightSide.capacity){
  51.         delete[] a;
  52.         a = new double[rightSide.capacity];
  53.     }
  54.     capacity = rightSide.capacity;
  55.     used = rightSide.used;
  56.     for (int i = 0; i < used; i++)
  57.     a[i] = rightSide.a[i];
  58.     return *this;
  59. }
  60. PFArrayD::~PFArrayD(){
  61.     delete[] a;
  62. }
  63. void testPFArrayD(){
  64.     int cap;
  65.     cout << "Enter capacity of this super array: ";
  66.     cin >> cap;
  67.     PFArrayD temp (cap);
  68.     cout << "Enter up to " << cap << " nonnegative numbers. \n";
  69.     cout << "Place a negative number at the end. \n";
  70.     double next;
  71.     cin >> next;
  72.     while ((next >= 0) && (!temp.full())) {
  73.         temp.addElement(next);
  74.         cin >> next;
  75.     }
  76.     cout << "You entered the following " << temp.getNumberUsed() << " numbers:\n";
  77.     int index;
  78.     int count = temp.getNumberUsed();
  79.     for (index = 0; index < count; index++)
  80.         cout << temp[index] << " ";
  81.     cout << endl;
  82.     cout << "(plus a sentinel value.)\n";
  83. }
  84. int main() {
  85.     cout << "This program tests the class PFArrayD.\n";
  86.     char ans;
  87.     do {
  88.         testPFArrayD();
  89.         cout << "Test again? (y/n) ";
  90.         cin >> ans;
  91.     } while ((ans == 'y') || (ans == 'Y'));
  92.     //system("pause");
  93.     return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement