1cutcut1

Untitled

Apr 29th, 2025
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.68 KB | None | 0 0
  1. 1)
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class Room {
  7. public:
  8. Room() { }
  9.  
  10. virtual void displayRoomInfo() = 0;
  11.  
  12. virtual void displayRoomPrice() = 0;
  13.  
  14. virtual ~Room() { }
  15. };
  16.  
  17. class StandardRoom : public Room
  18. {
  19. private:
  20. bool hasBathroom;
  21.  
  22. public:
  23. StandardRoom()
  24. {
  25. this->hasBathroom = false;
  26. }
  27.  
  28. StandardRoom(bool hasBathroom)
  29. {
  30. this->hasBathroom = hasBathroom;
  31. }
  32.  
  33. void displayRoomInfo()
  34. {
  35. cout << "This is a standard room with a queen-sized bed";
  36. if(hasBathroom) cout << " and a bathroom";
  37. cout << ".\n";
  38. }
  39.  
  40. void displayRoomPrice()
  41. {
  42. cout << "The price for a standard room is $";
  43. if(hasBathroom) cout << "100";
  44. else cout << "80";
  45. cout << " per night.\n";
  46. }
  47.  
  48. ~StandardRoom() { }
  49. };
  50.  
  51. class DeluxeRoom : public Room {
  52. bool hasBalcony;
  53.  
  54. public:
  55. DeluxeRoom() { this->hasBalcony = false; }
  56.  
  57. DeluxeRoom(bool hasBalcony) { this->hasBalcony = hasBalcony; }
  58.  
  59. void displayRoomInfo() {
  60. cout << "This is a deluxe room with a king-sized bed, a bathroom, a mini-fridge";
  61. if(hasBalcony) cout << " and a balcony";
  62. cout << ".\n";
  63. }
  64.  
  65. void displayRoomPrice() {
  66. cout << "The price for a deluxe room is $";
  67. if(hasBalcony) cout << "200";
  68. else cout << "160";
  69. cout << " per night.\n";
  70. }
  71.  
  72. ~DeluxeRoom() { }
  73. };
  74.  
  75. int main() {
  76. Room* rooms[5];
  77.  
  78. int testCase;
  79.  
  80. cin >> testCase;
  81.  
  82. if (testCase == 1) {
  83. cout << "TEST CASE 1: TESTING STANDARD ROOM CLASS" << endl;
  84.  
  85. for (int i = 0; i < 5; i++) {
  86. rooms[i] = new StandardRoom();
  87. rooms[i]->displayRoomInfo();
  88. rooms[i]->displayRoomPrice();
  89. }
  90. } else if (testCase == 2) {
  91. cout << "TEST CASE 2: TESTING DELUXE ROOM CLASS" << endl;
  92. for (int i = 0; i < 5; i++) {
  93. rooms[i] = new DeluxeRoom();
  94. rooms[i]->displayRoomInfo();
  95. rooms[i]->displayRoomPrice();
  96. }
  97. } else {
  98. cout << "TEST CASE 3: TESTING BOTH CLASSES" << endl;
  99. for (int i = 0; i < 5; i++) {
  100. if (i % 2) {
  101. bool hasBalcony;
  102. cin >> hasBalcony;
  103. rooms[i] = new DeluxeRoom(hasBalcony);
  104. }
  105. else {
  106. bool hasBathroom;
  107. cin >> hasBathroom;
  108. rooms[i] = new StandardRoom(hasBathroom);
  109. }
  110. rooms[i]->displayRoomInfo();
  111. rooms[i]->displayRoomPrice();
  112. }
  113. }
  114.  
  115. return 0;
  116. }
  117. -------------------------------------------------------------------------------------------------------------------------------
  118. 2)
  119. #include <iostream>
  120. #include <cstring>
  121.  
  122. using namespace std;
  123. class RCPlane
  124. {
  125. protected:
  126. char *model;
  127. char proizvoditel[50];
  128. float osnovnatezhina;
  129. public:
  130. RCPlane()
  131. {
  132. this->model=new char[0];;
  133. }
  134. RCPlane(char *model,char *proizvoditel,float osnovnatezhina)
  135. {
  136. this->model=new char[strlen(model)+1];
  137. strcpy(this->model,model);
  138. strcpy(this->proizvoditel,proizvoditel);
  139. this->osnovnatezhina=osnovnatezhina;
  140. }
  141. virtual float calculateTotalWeight()=0;
  142. virtual void printInfo()=0;
  143. virtual ~RCPlane()
  144. {
  145. delete []model;
  146. }
  147. };
  148. class RCRacingPlane : public RCPlane
  149. {
  150. private:
  151. int brservo;
  152. public:
  153. RCRacingPlane():RCPlane() {}
  154. RCRacingPlane(char *model,char *proizvoditel,float osnovnatezhina,int brservo) : RCPlane(model,proizvoditel,osnovnatezhina)
  155. {
  156. this->brservo=brservo;
  157. }
  158. float calculateTotalWeight()
  159. {
  160.  
  161. if(brservo>4)
  162. {
  163.  
  164. int temp=brservo-4;
  165. return osnovnatezhina +9*temp;
  166. }
  167. else
  168. {
  169. return osnovnatezhina;
  170. }
  171. }
  172. void printInfo()
  173. {
  174. cout<<model<<", "<<proizvoditel<<", "<<brservo<<" - "<<calculateTotalWeight()<<endl;
  175. }
  176. ~RCRacingPlane() {}
  177. };
  178. class RCGlider : public RCPlane
  179. {
  180. private:
  181. bool sobiranje_p;
  182. public:
  183. RCGlider():RCPlane() {}
  184. RCGlider(char *model,char *proizvoditel,float osnovnatezhina,bool sobiranje_p) : RCPlane(model,proizvoditel,osnovnatezhina)
  185. {
  186. this->sobiranje_p=sobiranje_p;
  187. }
  188.  
  189. float calculateTotalWeight()
  190. {
  191. if(sobiranje_p)
  192. {
  193. // osnovna+=osnovna+20;
  194. return osnovnatezhina +20;
  195. }
  196. else
  197. {
  198. return osnovnatezhina;
  199. }
  200. }
  201.  
  202. void printInfo()
  203. {
  204. cout<<model<<", "<<proizvoditel<<", "<<sobiranje_p<<" - "<<calculateTotalWeight()<<endl;
  205. }
  206. ~RCGlider() {}
  207. };
  208. float heaviestRacingPlane(RCPlane *p[], int n)
  209. {
  210. float max=p[0]->calculateTotalWeight();
  211. for(int i=0;i<n;i++)
  212. {
  213.  
  214. if(p[i]->calculateTotalWeight()>max)
  215. {
  216. max=p[i]->calculateTotalWeight();
  217. }
  218. }
  219. return max;
  220.  
  221. }
  222.  
  223. int main()
  224. {
  225. int n;
  226. //cout << "Enter the number of planes: ";
  227. cin >> n;
  228.  
  229. RCPlane* planes[n];
  230.  
  231. for (int i = 0; i < n; ++i)
  232. {
  233. char type[30];
  234. char model[30];
  235. char manufacturer[30];
  236. float basicWeight;
  237. int numberOfServos;
  238. bool foldingPropeller;
  239.  
  240. //cout << "Enter plane type (RCRacingPlane/RCGlider): ";
  241. cin >> type;
  242.  
  243. //cout << "Enter model: ";
  244. cin >> model;
  245.  
  246. //cout << "Enter manufacturer: ";
  247. cin >> manufacturer;
  248.  
  249. //cout << "Enter basic weight: ";
  250. cin >> basicWeight;
  251.  
  252. if (strcmp(type, "RCRacingPlane") == 0)
  253. {
  254. //cout << "Enter numberOfServos: ";
  255. cin >> numberOfServos;
  256. planes[i] = new RCRacingPlane(model, manufacturer, basicWeight, numberOfServos);
  257. }
  258. else if (strcmp(type, "RCGlider") == 0)
  259. {
  260. //cout << "Does it have folding propeller (0/1): ";
  261. cin >> foldingPropeller;
  262. planes[i] = new RCGlider(model, manufacturer, basicWeight, foldingPropeller);
  263. }
  264. }
  265.  
  266. RCPlane* heaviestPlane = nullptr;
  267. float maxValue = heaviestRacingPlane(planes, n);
  268.  
  269. for (int i = 0; i < n; ++i)
  270. {
  271. if (planes[i]->calculateTotalWeight() == maxValue)
  272. {
  273. heaviestPlane = planes[i];
  274. break;
  275. }
  276. }
  277.  
  278. if (heaviestPlane != nullptr)
  279. {
  280. cout << "Heaviest RCPlane:\n";
  281. heaviestPlane->printInfo();
  282. }
  283.  
  284. // Deallocate memory
  285. for (int i = 0; i < n; ++i)
  286. {
  287. delete planes[i];
  288. }
  289.  
  290. return 0;
  291. }
Advertisement
Add Comment
Please, Sign In to add comment