fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. class Cook {
  5. char* name;
  6. char* surname;
  7.  
  8. public:
  9. Cook();
  10. Cook(char*, char*);
  11. Cook(Cook&);
  12. ~Cook();
  13. char* getName();
  14. char* getSurname();
  15. Cook& setName(char*);
  16. Cook& setSurname(char*);
  17. void print();
  18. };
  19. Cook::Cook() {
  20. name = new char[20];
  21. strcpy(name, "No_name");
  22. surname = new char[20];
  23. strcpy(surname, "No_surname");
  24. };
  25.  
  26. Cook::Cook(char* fname, char* fsurname)
  27. {
  28. name = new char[strlen(fname) + 1];
  29. strcpy(name, fname);
  30. surname = new char[strlen(fsurname) + 1];
  31. strcpy(surname, fsurname);
  32. };
  33.  
  34. Cook::Cook(Cook& other) {
  35. name = new char[strlen(other.name) + 1];
  36. strcpy(name, other.name);
  37. surname = new char[strlen(other.surname) + 1];
  38. strcpy(surname, other.surname);
  39. };
  40.  
  41. Cook::~Cook() {
  42. delete[] name;
  43. delete[] surname;
  44. };
  45.  
  46. char* Cook::getName() {
  47. return name;
  48. };
  49.  
  50. char* Cook::getSurname() {
  51. return surname;
  52. };
  53. Cook& Cook::setName(char* fname) {
  54. delete[] name;
  55. name = new char[strlen(fname) + 1];
  56. strcpy(name, fname);
  57. return *this;
  58. };
  59.  
  60. Cook& Cook::setSurname(char* fsurname) {
  61. delete[] surname;
  62. surname = new char[strlen(fsurname) + 1];
  63. strcpy(surname, fsurname);
  64. return *this;
  65. };
  66.  
  67. void Cook::print() {
  68. cout << "Cook:" << " " << name << " " << surname << endl;
  69. };
  70.  
  71.  
  72.  
  73. class Dish {
  74. Cook chef;
  75. char* name_dish;
  76. int price;
  77.  
  78. public:
  79. Dish();
  80. Dish(char*, char*, char*, int);
  81. Dish(Dish&);
  82. ~Dish();
  83. char* getName();
  84. int getPrice();
  85. Cook& getChef();
  86. Dish& setNameDish(char*);
  87. Dish& setPrice(int);
  88. Dish& setChef(Cook&);
  89. void printDish();
  90.  
  91. };
  92.  
  93. Dish::Dish() : chef(), price(0) {
  94. name_dish = new char[20];
  95. strcpy(name_dish, "No_name");
  96. };
  97.  
  98. Dish::Dish(char* fname, char* fsurname, char* fname_dish, int fprice) :chef(fname, fsurname), price(fprice)
  99. {
  100. name_dish = new char[strlen(fname_dish) + 1];
  101. strcpy(name_dish, fname_dish);
  102. };
  103.  
  104. Dish::Dish(Dish& other) : chef(other.chef), price(other.price) {
  105. name_dish = new char[strlen(other.name_dish) + 1];
  106. strcpy(name_dish, other.name_dish);
  107. };
  108.  
  109. Dish::~Dish() {
  110. delete[] name_dish;
  111. };
  112.  
  113. char* Dish::getName() {
  114. return name_dish;
  115. };
  116.  
  117. int Dish::getPrice() {
  118. return price;
  119. };
  120.  
  121. Cook& Dish::getChef() {
  122. return chef;
  123. };
  124.  
  125. Dish& Dish::setNameDish(char* fname_dish) {
  126. delete[] name_dish;
  127. name_dish = new char[strlen(fname_dish) + 1];
  128. strcpy(name_dish, fname_dish);
  129. return *this;
  130. };
  131.  
  132. Dish& Dish::setPrice(int fprice) {
  133. price = fprice;
  134. return *this;
  135. };
  136.  
  137. Dish& Dish::setChef(Cook& fchef) {
  138. chef = fchef;
  139. return *this;
  140. };
  141.  
  142. void Dish::printDish() {
  143. cout << "Dish:" << " " << name_dish << " " << price << " " << chef.getName() << " " << chef.getSurname() << endl;
  144. };
  145.  
  146.  
  147.  
  148. int main()
  149. {
  150.  
  151. int fprice;
  152. char fname_dish[30];
  153. char fname[30];
  154. char fsurname[30];
  155. Cook obj1;
  156.  
  157. cout << "Enter name of chef: " << endl;
  158. cin >> fname;
  159. cout << "Enter surname of chef: " << endl;
  160. cin >> fsurname;
  161. Cook obj2(fname, fsurname);
  162. Cook obj3 = obj2;
  163. cout << "This is objects before change: " << endl;
  164. obj1.print();
  165. obj2.print();
  166. obj3.print();
  167.  
  168.  
  169.  
  170. Dish dish1;
  171. cout << "Enter name of Dish: " << endl;
  172. cin >> fname_dish;
  173. cout << "Enter price of dish: " << endl;
  174. cin >> fprice;
  175. Dish dish2(fname, fsurname, fname_dish, fprice);
  176. Dish dish3 = dish2;
  177. cout << "This is dishes before change: " << endl;
  178. dish1.printDish();
  179. dish2.printDish();
  180. dish3.printDish();
  181.  
  182.  
  183. cout << "New name of dish for obj3:" << endl;
  184. cin >> fname_dish;
  185. cout << "Enter new price for obj3:" << endl;
  186. cin >> fprice;
  187. cout << "Enter new name of chef obj3:" << endl;
  188. cin >> fname;
  189. cout << "Enter new surname of chef obj3:" << endl;
  190. cin >> fsurname;
  191. obj3.setName(fname).setSurname(fsurname);
  192. cout << "This is obj3 after change:" << endl;
  193.  
  194. dish3.setNameDish(fname_dish).setPrice(fprice).setChef(obj3);
  195. cout << "This is dish3 after change:" << endl;
  196. dish1.printDish();
  197. dish2.printDish();
  198. dish3.printDish();
  199.  
  200. return 0;
  201. }
Runtime error #stdin #stdout #stderr 0.01s 5384KB
stdin
Standard input is empty
stdout
Enter name of chef: 
Enter surname of chef: 
This is objects before change: 
Cook:  No_name  No_surname
Cook:  �  �a�U
Cook:  �  �a�U
Enter name of Dish: 
Enter price of dish: 
This is dishes before change: 
Dish: No_name  0 No_name No_surname
Dish:  O���  5357 � �a�U
Dish:  O���  5357 � �a�U
New name of dish for obj3:
Enter new price for obj3:
Enter new name of chef obj3:
Enter new surname of chef obj3:
This is obj3 after change:
This is dish3 after change:
Dish: No_name  0 No_name No_surname
Dish:  O���  5357 � �a�U
Dish:  O���  5357 � �a�U
stderr
free(): double free detected in tcache 2