C++ Class Static member variable

Objectives :

  • myClassStaticMember1.cp
    All the objects of a class can access the value of a static member. Unlike other members, the static members must be defined outside of the class
  • The Static Data member can be any type, can't be mutated and
  • Code: myClassStaticMember1.txt
 

Step: 1 Create a source file.

Step: 2 Edit and save Source file

a) Static members must be defined before use . In the current code snippet, the line was marked out, and note the error message "undefined reference to Base::mystat".

b) In the following code, static member was defined.

c) Edited code module:

Step: 3 Runtime Views:

All the variables are stored separately at their designated addresses. The static members can be accessed any where in the program.

 

Step: