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

Class Box

Examples/NoModules/Chapter 13/Ex13_03/Box.h:6–40  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4#include <compare> // For std::partial_ordering (see Chapter 4)
5
6class Box
7{
8public:
9 // Constructors
10 Box() = default;
11 Box(double l, double w, double h) : m_length{ l }, m_width{ w }, m_height{ h } {}
12
13 double volume() const { return m_length * m_width * m_height; }
14
15 // Accessors
16 double getLength() const { return m_length; }
17 double getWidth() const { return m_width; }
18 double getHeight() const { return m_height; }
19
20 std::partial_ordering operator<=>(const Box& otherBox) const
21 {
22 return volume() <=> otherBox.volume();
23 }
24 std::partial_ordering operator<=>(double otherVolume) const
25 {
26 return volume() <=> otherVolume;
27 }
28
29 bool operator==(const Box& otherBox) const
30 {
31 return m_length == otherBox.m_length
32 && m_width == otherBox.m_width
33 && m_height == otherBox.m_height;
34 }
35
36private:
37 double m_length{ 1.0 };
38 double m_width{ 1.0 };
39 double m_height{ 1.0 };
40};
41
42#endif

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected