-
Notifications
You must be signed in to change notification settings - Fork 549
/
Copy path13_homework_02_answer.cpp
111 lines (83 loc) · 2.35 KB
/
13_homework_02_answer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <bits/stdc++.h>
using namespace std;
class Reservation {
public:
virtual double TotalCost() const = 0;
virtual Reservation* Clone() const = 0;
virtual ~Reservation() {
}
};
class FlightReservation: public Reservation {
private:
// Some data
public:
FlightReservation(string several_parms = "") {
}
virtual FlightReservation* Clone() const override {
return new FlightReservation(*this);
}
virtual double TotalCost() const override {
return 2000;
}
};
class HotelReservation: public Reservation {
private:
int price_per_night;
int total_nights;
public:
HotelReservation(int price_per_night, int total_nights) :
price_per_night(price_per_night), total_nights(total_nights) {
}
virtual HotelReservation* Clone() const override {
return new HotelReservation(*this);
}
virtual double TotalCost() const override {
return price_per_night * total_nights;
}
};
class ItineraryReservation: public Reservation {
protected:
vector<Reservation*> reservations; // As has pointers, we need copy constructor
public:
ItineraryReservation() {
}
ItineraryReservation(const ItineraryReservation& another_reservation) { // copy constructor
for (const Reservation* reservation : another_reservation.GetReservations())
AddReservation(*reservation);
}
void AddReservation(const Reservation& reservation) {
reservations.push_back(reservation.Clone());
}
virtual double TotalCost() const {
double cost = 0;
for (const Reservation* reservation : reservations)
cost += reservation->TotalCost();
return cost;
}
~ItineraryReservation() {
Clear();
}
const vector<Reservation*>& GetReservations() const {
return reservations;
}
void Clear() {
for (const Reservation* reservation : reservations)
delete reservation;
reservations.clear();
}
virtual Reservation* Clone() const override {
return new ItineraryReservation(*this);
}
};
ItineraryReservation Make_ititinary() {
ItineraryReservation itinerary;
itinerary.AddReservation(FlightReservation());
itinerary.AddReservation(FlightReservation());
itinerary.AddReservation(HotelReservation(50, 2));
return itinerary;
}
int main() {
ItineraryReservation itinerary = Make_ititinary();
cout << itinerary.TotalCost(); // 4100 = 2000 + 2000 + 100
return 0;
}