HackerRank C++ Hotel Prices




In this challenge, the task is to debug the existing code to successfully execute all provided test files.

The given code defines two classes HotelRoom and HotelApartment denoting respectively a standard hotel room and a hotel apartment. An instance of any of these classes has two parameters: bedrooms and bathrooms denoting respectively the number of bedrooms and the number of bathrooms in the room.



  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. class HotelRoom {
  5. public:
  6. HotelRoom(int bedrooms, int bathrooms)
  7. : bedrooms_(bedrooms), bathrooms_(bathrooms) {}
  8. int get_price() {
  9. return 50*bedrooms_ + 100*bathrooms_;
  10. }
  11. private:
  12. int bedrooms_;
  13. int bathrooms_;
  14. };
  15. class HotelApartment : public HotelRoom {
  16. public:
  17. HotelApartment(int bedrooms, int bathrooms)
  18. : HotelRoom(bedrooms + 2, bathrooms) {}
  19. int get_price() {
  20. return HotelRoom::get_price() + 100;
  21. }
  22. };
  23. int main() {
  24. int n;
  25. cin >> n;
  26. vector<HotelRoom*> rooms;
  27. for (int i = 0; i < n; ++i) {
  28. string room_type;
  29. int bedrooms;
  30. int bathrooms;
  31. cin >> room_type >> bedrooms >> bathrooms;
  32. if (room_type == "standard") {
  33. rooms.push_back(new HotelRoom(bedrooms, bathrooms));
  34. } else {
  35. rooms.push_back(new HotelApartment(bedrooms, bathrooms));
  36. }
  37. }
  38. int total_profit = 0;
  39. for (auto room : rooms) {
  40. total_profit += room->get_price();
  41. }
  42. cout << total_profit << endl;
  43. for (auto room : rooms) {
  44. delete room;
  45. }
  46. rooms.clear();
  47. return 0;
  48. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube