MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / main

Function main

Examples/NoModules/Chapter 15/Ex15_07/Ex15_07.cpp:9–30  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7#include "Carton.h" // For the Carton class
8
9int 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}

Callers

nothing calls this directly

Calls 2

push_backMethod · 0.45
showVolumeMethod · 0.45

Tested by

no test coverage detected