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

Class Box

Examples/NoModules/Chapter 14/Ex14_03/Box.h:8–35  ·  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 double volume() const { return m_length * m_width * m_height; }
25
26 // Accessors
27 double getLength() const { return m_length; }
28 double getWidth() const { return m_width; }
29 double getHeight() const { return m_height; }
30
31protected: // Protected to facilitate further examples
32 double m_length {1.0}; // later this chapter (should normally be private)
33 double m_width {1.0};
34 double m_height {1.0};
35};
36
37// Stream output for Box objects
38inline std::ostream& operator<<(std::ostream& stream, const Box& box)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected