Advertisement
Guest User

MyStaticArray.h

a guest
Feb 25th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. // Written by Karl Hou
  2. // lab 5a, The "MyStaticArray" program
  3.  
  4. #ifndef MyStaticArray_H
  5. #define MyStaticArray_H
  6.  
  7. template <typename V, int CAPACITY>
  8. class Array{
  9. public:
  10. Array();
  11. inline V& operator[](int);// Setter function, use const if you want a getter
  12. int getCapacity() const; // Returns max data size
  13. int scan(const V&) const; // Search for a data value
  14.  
  15. private:
  16. V data[CAPACITY];
  17. int index;
  18. V dummy;
  19. };
  20.  
  21. template<typename V, int CAPACITY> // Default Constructor
  22. Array<V, CAPACITY>::Array() {
  23. }
  24.  
  25. template<typename V, int CAPACITY> //Setter
  26. inline V& Array<V, CAPACITY>::operator[](int index) {
  27. if (index < 0)
  28. return dummy;
  29.  
  30. if (index >= CAPACITY)
  31. return dummy;
  32.  
  33. return data[index];
  34. }
  35.  
  36. template<typename V, int CAPACITY> // Capacity returner
  37. int Array<V, CAPACITY>::getCapacity() const {
  38. return CAPACITY;
  39. }
  40.  
  41. template<typename V, int CAPACITY> // A search function, returns -1 if not found.
  42. int Array<V, CAPACITY>::scan(const V& arg) const {
  43. for (int i = 0; i < CAPACITY; i++) {
  44. if(arg == data[i]) {
  45. return i;
  46. }
  47. }
  48. return -1;
  49. }
  50.  
  51. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement