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