Skip to content

Commit 38f37ef

Browse files
Create Hotel_Prices.cpp
1 parent c8a98ac commit 38f37ef

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

Hackerrank/c++/Hotel_Prices.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
C++14 Solution to the problem link: https://www.hackerrank.com/challenges/hotel-prices/problem
3+
*/
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
class HotelRoom {
11+
public:
12+
HotelRoom(int bedrooms, int bathrooms)
13+
: bedrooms_(bedrooms), bathrooms_(bathrooms) {}
14+
15+
int get_price() {
16+
return 50*bedrooms_ + 100*bathrooms_;
17+
}
18+
private:
19+
int bedrooms_;
20+
int bathrooms_;
21+
};
22+
23+
class HotelApartment : public HotelRoom {
24+
public:
25+
HotelApartment(int bedrooms, int bathrooms)
26+
: HotelRoom(bedrooms, bathrooms) {}
27+
28+
int get_price() {
29+
return HotelRoom::get_price() + 100;
30+
}
31+
};
32+
33+
int main() {
34+
int n;
35+
cin >> n;
36+
vector<HotelRoom*> rooms;
37+
for (int i = 0; i < n; ++i) {
38+
string room_type;
39+
int bedrooms;
40+
int bathrooms;
41+
cin >> room_type >> bedrooms >> bathrooms;
42+
if (room_type == "standard") {
43+
rooms.push_back(new HotelRoom(bedrooms, bathrooms));
44+
} else {
45+
rooms.push_back(new HotelApartment(bedrooms, bathrooms));
46+
}
47+
}
48+
49+
int total_profit = 0;
50+
for (auto room : rooms) {
51+
total_profit += room->get_price();
52+
}
53+
cout << total_profit << endl;
54+
55+
for (auto room : rooms) {
56+
delete room;
57+
}
58+
rooms.clear();
59+
60+
return 0;
61+
}

0 commit comments

Comments
 (0)