hi guys. I was writing code but i cant figure out how to write one function.
This is my question:
Define a class Recipe that keeps info for number of ingredients (integer), name (char array of max 100) and contents of the recipe (dynamically allocated array of characters) (5 points). For this class overload:
operator ++ (prefix and postfix notation) - that increments number of ingredients (10 points)
operator << for printing all data of the recipe in new lines (5 points)
operator == for comparison of two recipes by their names (5 points).
Than define a class RecipesBook that has a name (max 100 chars), dynamically allocated array of objects of class Recipe and number of elements in the array (integer) (5 points). For the class implement:
operator += adding new recipe in the array of recipes, only if the recipe with same name does not exists (10 points)
operator << for printing the name of the book with all its recipes (5 points)
function with definition: RecipesBook newBook(Recipe &r). This function creates new book with recipes that will have the same name and will contain all the recipes that have less ingredients than the passed recipe r. All recipes in the new book should have incremented ingredients by 1. (10 points)
Implement other functions so the program compiles and it's valid. (5 points)
This is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
#include <iostream>
#include <cstring>
#include <string.h>
using namespace std;
class Recipe
{
private:
int num_ing;
char name[50];
char *contents;
public:
Recipe(int num_ing=0, char *name="", char *contents="")
{
this->num_ing=num_ing;
strcpy(this->name,name);
contents=new char[strlen(contents)+1];
strcpy(this->contents,contents);
}
Recipe(const Recipe &r)
{
num_ing=r.num_ing;
strcpy(name,r.name);
contents=new char[strlen(r.contents)+1];
strcpy(contents,r.contents);
}
Recipe &operator=(const Recipe &r)
{
if(this==&r)return *this;
num_ing=r.num_ing;
strcpy(name,r.name);
delete[]contents;
contents=new char[strlen(r.contents)+1];
strcpy(contents,r.contents);
return *this;
}
~Recipe()
{
delete[]contents;
}
char *getname()
{
return name;
}
friend ostream &operator <<(ostream &out , const Recipe &r )
{
out<<r.num_ing;
out<<r.name;
out<<r.contents;
return out;
}
bool operator ==(const Recipe &r)
{
return strcmp(name,r.name)==0;
}
Recipe &operator++()
{
num_ing++;
return *this;
}
void set_number_ingredients(int num_ing)
{
this->num_ing=num_ing;
}
void set_name(char *name)
{
strcpy(this->name,name);
}
void set_contents(char *contents)
{
strcpy(this->contents,contents);
}
};
class RecipesBook
{
private:
int num;
char name[50];
Recipe *rec;
public:
RecipesBook(char *name="")
{
strcpy(this->name,name);
num=0;
}
RecipesBook(const RecipesBook &rb)
{
strcpy(name,rb.name);
num=rb.num;
rec=new Recipe[rb.num];
for(int i=0; i<num; i++)
{
rec[i]=rb.rec[i];
}
}
RecipesBook &operator=(const RecipesBook &rb)
{
if(this==&rb)return *this;
strcpy(name,rb.name);
num=rb.num;
delete[]rec;
rec=new Recipe[rb.num];
for(int i=0; i<num; i++)
{
rec[i]=rb.rec[i];
}
}
~RecipesBook()
{
delete[]rec;
}
friend ostream &operator<<(ostream &out, const RecipesBook &rb)
{
out<<rb.name;
for(int i=0; i<rb.num; i++)
{
out<<rb.rec[i]<<endl;
}
return out;
}
RecipesBook &operator+=(const Recipe &r)
{
bool notsame = true;
for (int i=0; i<num; i++){
if(rec[i]==r)
{
notsame=false;
break;
}
}
if(notsame)
{
Recipe *temp=new Recipe[num+1];
for(int i=0; i<num; i++)
{
temp[i]=rec[i];
}
delete[]rec;
rec=temp;
rec[num]=r;
num++;
return *this;
}
}
RecipesBook newBook(Recipe &r)
{
}
};
int main() {
Recipe rec;
int n;
char name[100], contents[200];
int num_ing;
cin >> name >> n;
RecipesBook b1(name);
for(int i = 0; i < n; i++){
cin >> num_ing >> name >> contents;
Recipe r(num_ing, name, contents);
b1 += r;
}
cout << b1;
cin >> num_ing >> name >> contents;
rec.set_number_ingredients(num_ing);
rec.set_name(name);
rec.set_contents(contents);
b1 += rec;
cout << b1;
RecipesBook b2 = b1.newBook(rec);
cout << b2;
return 0;
}
|
i cant figure out newBook function. I dont know exactly what im asked to do..