fork download
  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. using namespace std;
  5.  
  6. template <class T> class Array
  7. {
  8. private:
  9. T* data;
  10. unsigned int size;
  11.  
  12. public:
  13. Array<T> (unsigned int x)
  14. {
  15. size = x;
  16. }
  17.  
  18. const T& operator[] (int index) const
  19. {
  20. if (index < 0||index >= size )
  21. throw domain_error("Array index out of bounds");
  22. return data[index];
  23. }
  24.  
  25. unsigned int getSize () { return size; }
  26.  
  27. void add (const T&, int index)
  28. {
  29. if (index < 0 || index >= size)
  30. throw domain_error("Array index out of bounds");
  31. else
  32. data[index] = T();
  33. }
  34.  
  35.  
  36. void display ()
  37. {
  38. cout << "[" << data[0];
  39. for (int i = 1; i < size ; ++i)
  40. cout << " , " <<data[i];
  41. cout << "]" << endl;
  42. }
  43.  
  44. };
  45.  
  46. int main ()
  47. {
  48. Array<int> arr(10);
  49.  
  50. for (int i = 0; i <(int)arr.getSize(); ++i)
  51. arr.add(i,i);
  52.  
  53. arr.display();
  54. cout << arr[0] << endl;
  55.  
  56. return 0;
  57. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout

Standard output is empty