Intermediate Business Programming with C++

Automatic & Static Member Data

In a previous lecture, the attributes of data were discussed. In that discussion, a storage class called automatic was defined. Data members (also called attributes) of a class are similar to automatic variables. Automatic attributes for classes:

However classes can have attributes that are similar to variable with static storage as well. Static attributes for classes:

For examples of static attributes see: bank6.cpp and bankactv.cpp

As indicated above, static attributes may also be accessed by the ClassName. However this access must still obey the access section in which the attribute is declared. For example staticAccount.cpp will not compile because the static attribute is declared in the private access section and the ClassName is attempting to access the attribute outside of the class. If the static attribute is declared in the public access section, then it can be accessed by ClassName outside of the class. For example see staticAccount2.cpp.

Static attributes are not a part of the memory of an object. Therefore when the function sizeof() is applied to an object of the class its size does not include the static attributes. In addition, when the sizeof() function is applied to the class the value returned is the sum of the sizes of the non-static members. Although a static attribute is a part of the class, its size is not included in the size of the class. See haim3.cpp.

Категории