Static Members OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani
Static Data Members Static data members hold global data that is common to all objects of the class. Ex: count of objects currently present, common data accessed by all objects, etc. Ex: We want all objects of a class to calculate interest at the rate of 4.5%. This data should be globally available to all objects of a class. 2 problems: i] This data cannot be a member of any object, because same value would have to be maintained for this data in all objects, which takes unnecessary space. ii] This data should not be stored in a global variable. Even non-member functions will be able to access this data.
Storing the data in a static variable of the class resolves this conflict. Static data members are members of the class(also called as class variables) and not of any object of the class, that is they are not contained inside any object. Declaration of static data member: by using the keyword static static data_type variable_name; Ex: class Account }; static float interest_rate; - - - - - - - - - //a static data member
When a static data member is declared inside a class, memory will not be allocated for it. Moreover, memory for a static data member will not get allocated when objects of the class are declared. This is because a static data member is not a member of any object. In order to allocate memory for static data member, it must be explicitly defined outside the class. Syntax: data_type class_name : : variable_name; Ex: float Account : : interest_rate; //memory is allocated, defined outside the class There is only one copy of static data member in the memory.(for all the objects of a class) Static data members do not increase the size of objects of the class.
class Account }; static float interest_rate; - - - - - - - - - //a static data member float Account : : interest_rate; //memory is allocated, defined outside the class The above statement initializes interest_rate to zero(by default). You can also assign different value to static data member(if needed).
class Account }; static float interest_rate; - - - - - - - - - //a static data member float Account : : interest_rate = 4.5; //memory is allocated, defined outside the class, //initialized with 4.5 Making static data members private prevents any change from non-member functions as only member functions can change the value of static data members.
Static data members can be of any type. Ex: class Account static float interest_rate; static char name[30]; - - - - - - - }; float Account : : interest_rate = 4.5; char Account : : name = The Rich and Poor Bank ;
Static data members of integer type can be initialized within the class itself if the need arises. Ex: class Account static int namelength = 30; //static int type, initialized within class static char name[namelength]; - - - - - - - }; int Account : : namelength; char Account : : name = The Rich and Poor Bank ;
The static data member that has been initialized inside the class must be still defined outside the class to allocate memory for it. Once the initial value has been supplied within the class, the static data member must not be re-initialized when it is defined. Non-integer static data members cannot be initialized within the class. Ex: class Account }; static char name[30] = The Rich and Poor Bank ; //ERROR
One static data member can directly refer to another without using the scope resolution operator. Ex: class Account static int namelength = 30; static char name[namelength]; - - - - - - - };
Member functions can refer to static data members directly. Ex: class Account static float interest_rate; public: void updatebalance(); - - - - - - }; float Account : : interest_rate = 4.5; void Acount : : updatebalance() if(end_of_year) balance + = balanace * interest_rate /100; }
There are some things static data members can do but non-static data members cannot. 1. A static data member can be of the same type as the class of which it is a member.
2. Static data members can appear as the default value for the formal arguments of member functions of its class. StaticDataMember.cpp
Static Member Functions A member function that is declared static has the following properties: A static function can have access to only other static members (functions or variables) declared in the same class. Prefixing the function prototype with the keyword static specifies it as a static member function. Ex: static return_type function_name(arguments if any); The keyword static should not reappear in the definition of the function. A static member function can be called using the class name(instead of its objects) as follows: class_name : : static_function_name();
Ex: class Account static float interest_rate; public: static void set_interest_rate(float); - - - - - - -- }; float Account : : interest_rate = 4.5; void Account : : set_interest_rate(float p) interest_rate = p; } static void set_interest_rate(float) - - - - - - }
The function can be called directly without an object. Account : : set_interest_rate(5); Accessing non-static data members through a static member function results in compile time errors. Static functions can access only static data members of the class. Static member functions can still be called with respect to objects. Account a1; a1. set_interest_rate(5); StaticMemberFunction.cpp
References Sourav Sahay, Objected Oriented Programming with C++ E Balagurusamy, Objected Oriented Programming with C++