diff --git a/CMakeLists.txt b/CMakeLists.txt index 488803ae4cf4f2e4155f7a871c122264c53e5401..a2eb145c536e44dcd52a647a19db0ab23132dc9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,11 @@ src/testCommandList.cpp target_include_directories("Info3_Praktikum-test" PUBLIC include/) +add_executable("Class-Member-Variable-demo" +src/class-member-variables.cpp +) + + # Doxygen documentation find_package(Doxygen) if (DOXYGEN_FOUND) diff --git a/src/class-member-variables.cpp b/src/class-member-variables.cpp new file mode 100644 index 0000000000000000000000000000000000000000..790a7056c68dcfa98054672487be1089c6a526d3 --- /dev/null +++ b/src/class-member-variables.cpp @@ -0,0 +1,34 @@ +#include <iostream> + +using std::cout; +using std::string; + +class ControlDeveloper +{ +private: + // class variable + // explicit "inline" to initialize in this translatin unit + inline static string CD_Class = "CLASS Control-Developer"; + // member variable + const string CD_Member = "MEMBER Control-Developer"; +public: + // declare static to be called without object + static string getCDClass() { return CD_Class; } + // regular getter + string getCDMember() { return CD_Member; } +}; + + +main() +{ + cout << "Demonstration of class vs. member variables:\n"; + cout << "Without constructing an object we can call the static getter from the CD namespace:\n"; + cout << "CD_Class: " << ControlDeveloper::getCDClass() << '\n'; + cout << "To be able to get the member variable we need to construct"; + cout << " an object as the value will be specifically created for every object.\n"; + ControlDeveloper cd = ControlDeveloper(); + cout << "We can now call the non static getter:\n"; + cout << "CD_Member: " << cd.getCDMember() << '\n'; + cout << "We can now also use the object to call the static getter (with the same effect):\n"; + cout << "CD_Class: " << cd.getCDClass() << '\n'; +} \ No newline at end of file