SHARE
TWEET

Untitled

a guest Feb 21st, 2020 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*     FILE: array7.cpp     */
  2. /* An array with bounds checking.
  3.     Assignment creates duplicate and deletes old
  4.     array of values. (Memory leak.)
  5. */
  6. #include <iostream>
  7. #include <cstdlib>
  8.  
  9. using namespace std;
  10. class safe_array {
  11.     float * ptr;
  12.     int size;
  13. public:
  14.     safe_array(int length);
  15.     safe_array(const safe_array & param);
  16.     ~safe_array() { delete[] ptr; }
  17.     float &operator[ ](int index);
  18.     safe_array &operator=(const safe_array & operand2);
  19.  
  20.     int getSize() const;
  21. };
  22.  
  23. safe_array::safe_array(int length)
  24. {
  25.     ptr = new float[size = length];
  26. }
  27.  
  28. safe_array::safe_array(const safe_array & param)
  29. {
  30.     ptr = new float[size = param.size];
  31.     for (int i = 0; i < size; i++)
  32.         ptr[i] = param.ptr[i];
  33. }
  34.  
  35. safe_array & safe_array::operator=(const safe_array & operand2)
  36. {
  37.     if (this != &operand2)
  38.     {
  39.         delete[] ptr;
  40.         ptr = new int[size = operand2.size];
  41.         for (int i = 0; i < size; i++)
  42.             ptr[i] = operand2.ptr[i];
  43.     }
  44.  
  45.     return *this;
  46. }
  47.  
  48. float& safe_array::operator[ ](int index)
  49. {
  50.     if (index < 0 || size - 1 < index)
  51.         abort();
  52.     return ptr[index];
  53. }
  54.  
  55. int safe_array::getSize() const
  56. {
  57.     return size;
  58. }
  59.  
  60. int main()
  61. {
  62.     int i;
  63.     safe_array ar(5);
  64.     safe_array ar2(7);
  65.  
  66.     for (i = 0; i < ar.getSize(); i++)
  67.         ar[i] = i;
  68.  
  69.     for (i = 0; i < ar2.getSize(); i++)
  70.         ar2[i] = i * 10;
  71.  
  72.     for (i = 0; i < ar.getSize(); i++)   // display array ar      
  73.         cout << "ar[" << i << "] = " << ar[i] << endl;
  74.     cout << endl;
  75.  
  76.     for (i = 0; i < ar2.getSize(); i++)   // display array ar2      
  77.         cout << "ar2[" << i << "] = " << ar2[i] << endl;   cout << endl;
  78.  
  79.     ar2 = ar;
  80.  
  81.     for (i = 0; i < ar2.getSize(); i++)   // display array ar2        
  82.         cout << "ar2[" << i << "] = " << ar2[i] << endl;   cout << endl;
  83.  
  84.     for (i = 0; i < ar.getSize(); i++)   // alter array ar        
  85.         ar[i] = i * 11;
  86.  
  87.     for (i = 0; i < ar.getSize(); i++)   // display array ar      
  88.         cout << "ar[" << i << "] = " << ar[i] << endl;
  89.     cout << endl;
  90.  
  91.     for (i = 0; i < ar2.getSize(); i++)   // display array ar2        
  92.         cout << "ar2[" << i << "] = " << ar2[i] << endl;
  93.  
  94.     return 0;
  95. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top