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

Class Box

Examples/NoModules/Chapter 13/Ex13_11/Box.h:8–43  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6#include <algorithm> // For the std::min()/max() function templates
7
8class Box
9{
10public:
11 Box() = default;
12 Box(double length, double width, double height)
13 : m_length{length}, m_width{width}, m_height{height} {};
14
15 double volume() const
16 {
17 return m_length * m_width * m_height;
18 }
19
20 int compare(const Box& box) const
21 {
22 if (volume() < box.volume()) return -1;
23 if (volume() == box.volume()) return 0;
24 return +1;
25 }
26
27 friend std::ostream& operator<<(std::ostream& out, const Box& box)
28 {
29 return out << std::format("Box({:.1f},{:.1f},{:.1f})", box.m_length, box.m_width, box.m_height);
30 }
31
32 Box operator+(const Box& aBox) const // Function to add two Box objects
33 {
34 return Box{ std::max(m_length, aBox.m_length),
35 std::max(m_width, aBox.m_width),
36 m_height + aBox.m_height };
37 }
38
39private:
40 double m_length {1.0};
41 double m_width {1.0};
42 double m_height {1.0};
43};
44
45#endif

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected