Pastebin
API
tools
faq
paste
Login
Sign up
SHARE
TWEET
Recipes
StefiIOE
Apr 11th, 2020
115
Never
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!
C++
4.35 KB
raw
download
clone
embed
print
report
RAW Paste Data
#include<iostream> #include<cstring> using namespace std; class Recipe { protected: int number; char name[100]; char *contents; void copy (const Recipe &r) { this->number=r.number; strcpy(this->name,r.name); this->contents=new char[strlen(r.contents)+1]; strcpy(this->contents, r.contents); } public: Recipe(int number = 0, char *name = "", char *contents = "") { this->number=number; strcpy(this->name,name); this->contents=new char[strlen(contents)+1]; strcpy(this->contents, contents); } Recipe(const Recipe &r) { copy(r); } Recipe & operator = (const Recipe & r) { if (this!=&r) { delete [] contents; copy(r); } return *this; } void set_num_ing(int number) { this->number=number; } void set_name(char *name) { strcpy(this->name,name); } void set_contents(char *contents) { this->contents=new char[strlen(contents)+1]; strcpy(this->contents,contents); } void print () { cout<<number<<endl; cout<<name<<endl; cout<<contents<<endl; } bool Eq (Recipe r) { return (strcmp(this->name,r.name)==0); } Recipe &operator++() { number++; return * this; } Recipe operator++(int) { Recipe copy (*this); ++number; return copy; } char *getName() { return name; } int getNumber() { return number; } friend class RecipesBook; }; class RecipesBook { protected: char name [100]; Recipe *recipes; int element; void copy(const RecipesBook &rb) { strcpy(this->name,rb.name); this->element=rb.element; this->recipes=new Recipe[element+1]; for(int i=0; i<element; i++) { recipes[i]=rb.recipes[i]; } } public: RecipesBook(char*name = " ") { strcpy(this->name,name); this->element = 0; this->recipes = new Recipe [0]; } RecipesBook(const RecipesBook &r) { copy(r); } RecipesBook &operator = (const RecipesBook &r) { if(this!=&r) { delete [] recipes; copy(r); } return *this; } void setName(char *name) { strcpy(this->name,name); } void add(Recipe &r) { for(int i = 0 ; i < element ; i ++) { if(recipes[i].Eq(r)) { return; } } Recipe *tmp = new Recipe[element+1]; for(int i = 0 ; i < element ; i++) { tmp[i]=recipes[i]; } tmp[element]=r; element++; delete[]recipes; recipes=tmp; } RecipesBook newBook( Recipe r) { RecipesBook NewBook; NewBook.setName(this->name); Recipe tmp; for(int i = 0 ; i < element ; i ++) { if(recipes[i].getNumber()<r.getNumber()) { tmp=recipes[i]; tmp++; NewBook.add(tmp); } } return NewBook; } void print() { cout<<name<<endl; for(int i = 0 ; i < element ; i++) { recipes[i].print(); } } ~RecipesBook() { delete[]recipes; } }; 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.add(r); last = r; } b1.print(); cin >> num_ing >> name >> contents; rec.set_num_ing(num_ing); rec.set_name(name); rec.set_contents(contents); // cout<<name; cout<<"test"<<endl; b1.add(rec); //cout<<"ss"<<endl; //cout<<"test"<<endl; b1.print(); // cout<<"test"<<endl; RecipesBook b2 = b1.newBook(rec); b2.print(); // testing copy constructor cout << "b2 copy" << endl; RecipesBook rb = b2; last.set_name("changed-name"); rb.add(last); rb.print(); cout << "original" << endl; b2.print(); return 0; }
Public Pastes
Template button ca...
YAML | 45 sec ago
Button card
YAML | 2 min ago
# py_to_cpp_testin...
Python | 22 min ago
Rust Select/fork
Rust | 25 min ago
# py_to_cpp.py
Python | 28 min ago
Test2
Lua | 32 min ago
Untitled
Diff | 33 min ago
10. Top Number Met...
C# | 48 min ago
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!