Automatic Static Member Data


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:

  • have limited scope within the class or block where their object is defined

  • are created when the object is defined

  • are explicitly initialized

  • exist only during the life of their object

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

  • have scope of the class.

  • are not attributes of any class objects.

  • must be explicitly initialized outside of the class' definition when the program is compiled.

  • exist during the life of the class' definition

  • may have its value changed by any method .

  • may have its value changed by direct use of the class name and the scope specifier operator as in the following statement: ClassName::staticAttribute = aValue;

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.




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net