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

Class Box

Examples/NoModules/Chapter 14/Ex14_06/Box.h:8–38  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6#include <format> // For string formatting
7
8class Box
9{
10public:
11 // Constructors
12 Box(double l, double w, double h) : m_length{l}, m_width{w}, m_height{h}
13 { std::cout << "Box(double, double, double) called.\n"; }
14
15 explicit Box(double side) : Box{side, side, side}
16 { std::cout << "Box(double) called.\n"; }
17
18 Box() { std::cout << "Box() called.\n"; } // Default constructor
19
20 // Copy constructor
21 Box(const Box& b) : m_length{ b.m_length }, m_width{ b.m_width }, m_height{ b.m_height }
22 { std::cout << "Box copy constructor" << std::endl; }
23
24 // Destructor
25 ~Box() { std::cout << "Box destructor" << std::endl; }
26
27 double volume() const { return m_length * m_width * m_height; }
28
29 // Accessors
30 double getLength() const { return m_length; }
31 double getWidth() const { return m_width; }
32 double getHeight() const { return m_height; }
33
34protected: // Protected to facilitate further examples
35 double m_length {1.0}; // later this chapter (should normally be private)
36 double m_width {1.0};
37 double m_height {1.0};
38};
39
40// Stream output for Box objects
41inline std::ostream& operator<<(std::ostream& stream, const Box& box)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected