| 7 | #include "Carton.h" // For the Carton class |
| 8 | |
| 9 | int main() |
| 10 | { |
| 11 | // Careful: this first attempt at a mixed collection is a bad idea (object slicing!) |
| 12 | std::vector<Box> boxes; |
| 13 | boxes.push_back(Box{20.0, 30.0, 40.0}); |
| 14 | boxes.push_back(ToughPack{20.0, 30.0, 40.0}); |
| 15 | boxes.push_back(Carton{20.0, 30.0, 40.0, "plastic"}); |
| 16 | |
| 17 | for (const auto& p : boxes) |
| 18 | p.showVolume(); |
| 19 | |
| 20 | std::cout << std::endl; |
| 21 | |
| 22 | // Next, we create a proper polymorphic vector<>: |
| 23 | std::vector<std::unique_ptr<Box>> polymorphicBoxes; |
| 24 | polymorphicBoxes.push_back(std::make_unique<Box>(20.0, 30.0, 40.0)); |
| 25 | polymorphicBoxes.push_back(std::make_unique<ToughPack>(20.0, 30.0, 40.0)); |
| 26 | polymorphicBoxes.push_back(std::make_unique<Carton>(20.0, 30.0, 40.0, "plastic")); |
| 27 | |
| 28 | for (const auto& p : polymorphicBoxes) |
| 29 | p->showVolume(); |
| 30 | } |
nothing calls this directly
no test coverage detected