A price label may be passed around for reading without giving callers permission to change it. You already know that const can make an object read-only. A class must also identify which member functions are safe to call on such an object.
The const after a member function's parameter list makes it a const member function: int read() const. It promises not to modify the object's ordinary data members through that call. This position matters. It is not a promise that the returned integer can never be changed; it describes access to the object on which the function is called.
Const member functions can be called on both const and non-const objects. A member function without that qualifier cannot be called on a const object, even if its body happens not to write anything. The compiler checks the declared interface rather than guessing your intent from the body.
#include <iostream>
class Label {
private:
int code;
public:
Label(int value) : code(value) {}
int read() const { return code; }
};
int main() {
const Label shelf(42);
std::cout << shelf.read() << '\n';
}
The example prints 42. Follow the corresponding call in the starter: show receives a const Price&, then calls price.cents(). That call is allowed because cents() already has the trailing const. You only need to replace its temporary return value. Keep both uses of const: one restricts access through the parameter, and the other declares that the member function supports that access.
value from the const member cents(). The supplied read-only reporting function should print Price cents: 275.Edit main.cpp, then press Run to inspect the Console. Press Submit to check your work. Keep the supplied probes in place.