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

Function main

Examples/Modules/Chapter 15/Ex15_07/Ex15_07.cpp:7–28  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5import boxes;
6
7int 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}

Callers

nothing calls this directly

Calls 2

push_backMethod · 0.45
showVolumeMethod · 0.45

Tested by

no test coverage detected