Skip to content
Snippets Groups Projects
Commit 62e2ec9c authored by tobiglaser's avatar tobiglaser
Browse files

adding static vs member demo

parent edda04bc
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment