Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef ARRAY_H
- #define ARRAY_H
- #include <iostream>
- #include <string>
- #include "Recipe.h"
- using namespace std;
- template <class T>
- class Array
- {
- public:
- Array();
- void add(Recipe*);
- T getRecipe(int);
- int getSize();
- //below are Array operator overloaded function predeclarations
- T& operator+=(Recipe&);
- T& operator-=(Recipe&);
- T& operator-=(Array&);
- T& operator+=(Array&);
- private:
- int size;
- T* recipes;
- };
- template <class T>
- Array<T>::Array()
- {
- recipes = new T[MAX_RECIPES];
- }
- template <class T>
- int Array<T>::getSize() { return size; }
- template <class T>
- T Array<T>::getRecipe(int i) { return recipes[i]; }
- template <class T>
- T& Array<T>::operator+=(Recipe& s)
- {
- add(&s);
- //we already have an add function, so use it and return *this for cascading
- return *recipes;
- }
- template <class T>
- T& Array<T>::operator+=(Array& s)
- {
- Recipe* tmp;
- for (int i=0;i<s.size;i++) {
- tmp = s.getRecipe(i);
- *this+=*tmp;
- }
- return *recipes;
- }
- template <class T>
- T& Array<T>::operator-=(Recipe& s)
- {
- Array newRecipeArr;
- int check =0;
- if (size == 0) {
- return *recipes;
- }
- for (int i=0;i<size;i++){
- /*iterate through the array of recipes
- add all elements of the old array to the new one
- except the element we are supposed to delete*/
- if (!((s.getName() == recipes[i]->getName())
- && (s.getIngredients() == recipes[i]->getIngredients())
- && (s.getInstructions() == recipes[i]->getInstructions())))
- { Recipe tmp = *recipes[i];
- newRecipeArr += *recipes[i];
- }
- }
- *this = newRecipeArr;
- //set the array recipes array to the new one
- return *recipes;
- }
- template <class T>
- T& Array<T>::operator-=(Array& s)
- { Recipe* tmp;
- for (int i=0;i<s.size;i++) {
- tmp = s.getRecipe(i);
- *this-=*tmp;
- }
- return *recipes;
- }
- template <class T>
- void Array<T>::add(Recipe* recipe)
- {
- if (size == MAX_RECIPES)
- return;
- recipes[size] = recipe;
- size++;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement