Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class Recipe {
- private:
- int numIng;
- char name[100];
- char *contents;
- public:
- // Default Constructor
- Recipe() { contents = NULL; }
- // Destructor
- ~Recipe() { delete[] contents; }
- // Regular Constructor
- Recipe(int ing, char *nam, char *con) {
- numIng = ing;
- strcpy(name, nam);
- contents = new char[strlen(con) + 1];
- strcpy(contents, con);
- }
- // Assignment operator overload
- Recipe &operator = (const Recipe &r){
- if(this == &r) { return *this; }
- numIng = r.numIng;
- strcpy(name, r.name);
- delete [] contents;
- contents = new char[strlen(r.contents) + 1];
- strcpy(contents, r.contents);
- return *this;
- }
- // Copy-constructor
- Recipe(const Recipe &r) {
- numIng = r.numIng;
- strcpy(name, r.name);
- contents = new char[strlen(r.contents) + 1];
- strcpy(contents, r.contents);
- }
- Recipe &operator++(int) { //PRE-FIX ++a
- ++numIng;
- return *this;
- }
- Recipe operator++() { //POST-FIX a++
- Recipe r(*this);
- ++numIng;
- return r;
- }
- //ostream <<
- friend ostream &operator<<(ostream &out, const Recipe &r) {
- out << r.numIng <<endl;
- out << r.name <<endl;
- out << r.contents <<endl << endl;
- return out;
- }
- bool operator == (const Recipe &rhs){
- return strcmp(name, rhs.name) == 0;
- }
- // Get Functions
- const int getNumIng(){ return numIng; }
- const char *getName(){ return name;}
- const char *getContents(){ return contents;}
- // Set Functions
- void setNumIng(const int ing) { numIng = ing; }
- void setName(const char* nam) { strcpy(name, nam); }
- void setContents(const char* con) {
- delete [] contents;
- contents = new char[strlen(con)+1];
- strcpy(contents, con); }
- };
- class RecipesBook {
- private:
- char name[100];
- Recipe *recipes;
- int len;
- void copyRecipeBooks(RecipesBook &dest, const RecipesBook &source){
- for (int i = 0; i < source.len; ++i) {
- dest.recipes[i] = source.recipes[i];
- }
- }
- void copyRecipes(Recipe dest[], const Recipe source[], int const len){
- for (int i = 0; i < len; ++i) {
- dest[i] = source[i];
- }
- }
- public:
- RecipesBook() { recipes = NULL; len = 0; }
- ~RecipesBook() { delete [] recipes; }
- // Regular Constructor
- RecipesBook (char *n){
- strcpy(name, n);
- recipes = NULL;
- len = 0;
- }
- // Assignment Operator
- RecipesBook &operator = (const RecipesBook &rb){
- if (this == &rb) { return *this; }
- strcpy(name, rb.name);
- delete [] recipes;
- recipes = new Recipe[rb.len+1];
- copyRecipeBooks(*this, rb);
- len = rb.len;
- return *this;
- }
- // Copy-Constructor
- RecipesBook(const RecipesBook &rb){
- strcpy(name, rb.name);
- recipes = new Recipe[rb.len + 1];
- copyRecipeBooks(*this, rb);
- len = rb.len;
- }
- // Operator +=
- RecipesBook &operator += (const Recipe &r){
- bool exist = false;
- // check if r is in the rb
- for (int i = 0; i < len; ++i) {
- if(recipes[i] == r){
- exist = true;
- }
- }
- if(!exist){
- Recipe *old = recipes;
- recipes = new Recipe[len+1];
- copyRecipes(recipes, old, len);
- recipes[len] = r;
- ++len;
- delete [] old;
- }
- return *this;
- }
- friend ostream &operator << (ostream &out, const RecipesBook &rb){
- out << rb.name <<endl;
- for (int i = 0; i < rb.len; ++i) {
- out << rb.recipes[i];
- }
- return out;
- }
- RecipesBook newBook(Recipe &r){
- RecipesBook book(name);
- for (int i = 0; i < len; ++i) {
- if(recipes[i].getNumIng() < r.getNumIng()){
- book += recipes[i];
- book.recipes[i].setNumIng(recipes[i].getNumIng()+1);
- }
- }
- return book;
- }
- };
- int main() {
- Recipe rec;
- int n;
- char name[100], contents[200];
- int num_ing;
- cin >> name >> n;
- RecipesBook b1(name);
- Recipe last;
- for (int i = 0; i < n; i++) {
- cin >> num_ing >> name >> contents;
- Recipe r(num_ing, name, contents);
- b1 += r;
- last = r;
- }
- cout << b1;
- cin >> num_ing >> name >> contents;
- rec.setNumIng(num_ing);
- rec.setName(name);
- rec.setContents(contents);
- b1 += rec;
- cout << b1;
- RecipesBook b2 = b1.newBook(rec);
- cout << b2;
- // testing copy constructor
- cout << "b2 copy" << endl;
- RecipesBook rb = b2;
- last.setName("changed-name");
- rb += last;
- cout << rb;
- cout << "original" << endl;
- cout << b2;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement