Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class MyDynamicArray {
  5.     private:
  6.     int capacity;
  7.     int nums;
  8.     int *array;
  9.     int safe;
  10.   public:
  11.  MyDynamicArray(); //Default Constructor. The array should be of size 2.
  12.  MyDynamicArray(int s);//For this constructor the array should be of size s.
  13.  ~MyDynamicArray(); //Destructor for the class.
  14.  int& operator[](int i); //Traditional [] operator. Should print a message if i is out of bounds and return a reference to a zero value.
  15.  void add(int v); //increases the size of the array by 1 and stores v there.
  16.  void del();// reduces the size of the array by 1.
  17.  int length(); //returns the length of the array.
  18.  int clear(); //Frees any space currently used and starts over with an array of size 2.
  19. };
  20. MyDynamicArray::MyDynamicArray()
  21. {
  22.     array = new int[2];
  23.     nums = 0;
  24.     capacity = 2;
  25.      cout << "constructor called." << endl;
  26.     return;
  27. }
  28. MyDynamicArray::MyDynamicArray(int s)
  29. {
  30.     array = new int[s];
  31.     capacity = s;
  32.     nums = s;
  33.      cout << "Special constructor called." << endl;
  34.     return;
  35. }
  36. MyDynamicArray::~MyDynamicArray()
  37. {
  38.      cout << "Destructor called." << endl;
  39.     delete array;
  40.    
  41. }
  42. int& MyDynamicArray:: operator[](int i)
  43. {
  44.     if(i> nums-1)
  45.     {
  46.         cout << "flag 1" <<endl;
  47.     }
  48.     return safe;
  49.     /*
  50.     if(i<= nums-1)
  51.     {
  52.         //In range
  53.         return safe;
  54.     }
  55.     cout << "Out of bounds reference : " << i << endl;
  56.     return safe;*/
  57. }
  58. void MyDynamicArray::add(int v)
  59. {
  60.    
  61.    
  62. }
  63. void MyDynamicArray::del()
  64. {
  65.    
  66.    
  67. }
  68. int MyDynamicArray::length()
  69. {
  70.    
  71.    
  72. }
  73. int MyDynamicArray::clear()
  74. {
  75.    
  76.    
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement