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 make age, firstname, and lastname 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)

Dependencies

cluster_cxx03 C++ cluster_cxx03_standard_library_miscellanea The Standard Library: Miscellaneous Topics cluster_cxx03_data_encapsulation Data Encapsulation cluster_cxx03_exercises_userdb Exercises: User Database cluster_c The C Programming Language cluster_c_introduction Introduction cxx03_introduction Introduction c_introduction_installation Installation cxx03_introduction->c_introduction_installation cxx03_standard_library_miscellanea_string std::string cxx03_data_encapsulation_ctor_custom Custom Constructor cxx03_data_encapsulation_classes_objects Classes and Objects cxx03_data_encapsulation_ctor_custom->cxx03_data_encapsulation_classes_objects cxx03_data_encapsulation_ctor_dtor More Constructors, Destructors cxx03_data_encapsulation_ctor_dtor->cxx03_data_encapsulation_ctor_custom cxx03_data_encapsulation_ctor_default Default Constructor cxx03_data_encapsulation_ctor_dtor->cxx03_data_encapsulation_ctor_default cxx03_data_encapsulation_classes_objects->cxx03_introduction cxx03_data_encapsulation_c Object Oriented Programming In Good Ol’ C cxx03_data_encapsulation_classes_objects->cxx03_data_encapsulation_c cxx03_data_encapsulation_ctor_default->cxx03_data_encapsulation_ctor_custom cxx03_data_encapsulation_c->cxx03_introduction cxx03_exercises_userdb_user_ctor Exercise: Transform struct User Into A Class cxx03_exercises_userdb_user_ctor->cxx03_standard_library_miscellanea_string cxx03_exercises_userdb_user_ctor->cxx03_data_encapsulation_ctor_custom cxx03_exercises_userdb_user_const_members Exercise: const Members cxx03_exercises_userdb_user_const_members->cxx03_data_encapsulation_ctor_dtor cxx03_exercises_userdb_user_const_members->cxx03_exercises_userdb_user_ctor