Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #ifndef ARRAY_H
  2. #define ARRAY_H
  3. #include <iostream>
  4. #include <string>
  5. #include "Recipe.h"
  6. using namespace std;
  7.  
  8. template <class T>
  9. class Array
  10. {
  11. public:
  12. Array();
  13. void add(Recipe*);
  14. T getRecipe(int);
  15. int getSize();
  16. //below are Array operator overloaded function predeclarations
  17. T& operator+=(Recipe&);
  18. T& operator-=(Recipe&);
  19. T& operator-=(Array&);
  20. T& operator+=(Array&);
  21.  
  22. private:
  23. int size;
  24. T* recipes;
  25. };
  26.  
  27. template <class T>
  28. Array<T>::Array()
  29. {
  30. recipes = new T[MAX_RECIPES];
  31. }
  32. template <class T>
  33. int Array<T>::getSize() { return size; }
  34. template <class T>
  35. T Array<T>::getRecipe(int i) { return recipes[i]; }
  36.  
  37. template <class T>
  38. T& Array<T>::operator+=(Recipe& s)
  39. {
  40. add(&s);
  41. //we already have an add function, so use it and return *this for cascading
  42. return *recipes;
  43. }
  44.  
  45. template <class T>
  46. T& Array<T>::operator+=(Array& s)
  47. {
  48. Recipe* tmp;
  49. for (int i=0;i<s.size;i++) {
  50. tmp = s.getRecipe(i);
  51. *this+=*tmp;
  52. }
  53. return *recipes;
  54. }
  55.  
  56. template <class T>
  57. T& Array<T>::operator-=(Recipe& s)
  58. {
  59. Array newRecipeArr;
  60. int check =0;
  61. if (size == 0) {
  62. return *recipes;
  63. }
  64. for (int i=0;i<size;i++){
  65. /*iterate through the array of recipes
  66. add all elements of the old array to the new one
  67. except the element we are supposed to delete*/
  68. if (!((s.getName() == recipes[i]->getName())
  69. && (s.getIngredients() == recipes[i]->getIngredients())
  70. && (s.getInstructions() == recipes[i]->getInstructions())))
  71. { Recipe tmp = *recipes[i];
  72. newRecipeArr += *recipes[i];
  73. }
  74. }
  75. *this = newRecipeArr;
  76. //set the array recipes array to the new one
  77. return *recipes;
  78.  
  79. }
  80.  
  81. template <class T>
  82. T& Array<T>::operator-=(Array& s)
  83. { Recipe* tmp;
  84. for (int i=0;i<s.size;i++) {
  85. tmp = s.getRecipe(i);
  86. *this-=*tmp;
  87. }
  88. return *recipes;
  89. }
  90.  
  91. template <class T>
  92. void Array<T>::add(Recipe* recipe)
  93. {
  94. if (size == MAX_RECIPES)
  95. return;
  96.  
  97. recipes[size] = recipe;
  98. size++;
  99. }
  100.  
  101. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement