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

Class Box

Examples/NoModules/Chapter 12/Ex12_10/Box.h:6–34  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4#include <iostream>
5
6class Box
7{
8public:
9 // Constructors
10 Box() = default;
11 Box(double length, double width, double height);
12
13 double volume() const; // Const function to calculate the volume of a box
14
15 // Non-const overloads (return references to dimension variable)
16 double& length() { std::cout << "non-const overload called\n"; return m_length; };
17 double& width() { std::cout << "non-const overload called\n"; return m_width; };
18 double& height() { std::cout << "non-const overload called\n"; return m_height; };
19
20 // Const overloads (return references to const variables)
21 const double& length() const { std::cout << "const overload called\n"; return m_length; };
22 const double& width() const { std::cout << "const overload called\n"; return m_width; };
23 const double& height() const { std::cout << "const overload called\n"; return m_height; };
24
25 // Attempt to return non-const references to member variables from const functions
26// double& length() const { return m_length; }; // This must not be allowed to compile!
27// double& width() const { return m_width; };
28// double& height() const { return m_height; };
29
30private:
31 double m_length{1.0};
32 double m_width {1.0};
33 double m_height{1.0};
34};
35
36#endif

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected