Exercise: const
Members¶
Description¶
class User
has public members age
, firstname
, and
lastname
. Everyone can read them which is ok architectonically
(the class has no secrets, and it is only there to hold these data.
What’s not ok, architectonically, is that everyone is allowed to overwrite any of the members.
Use the
const
keyword to makeage
,firstname
, andlastname
read-only.Try to compile - this will fail.
Try to understand the compiler’s error messages (many). Hint: what it tries to say is that you have to initialize the members, rather than assign to them.
⟶ use an initializer list (see More Constructors, Destructors)