What are static Data and Static Member Functions in C++ ?
![]() Don't want to miss a single bit? Subscribe By Email for Daily Jobs |
Static data members are data and functions that are associated with the class itself, rather than with the objects of the class. For example a count is needed of how many objects of a particular class type have been created at any one point in the program.
A static data member acts as a global object that belongs to its class type. Unlike other data members where each class object has its own copy, there is only one copy of a static data member per class type. A static data member is a single, shared object accessible to all objects of its class type.
A data member is made static by prefixing the data member declaration within the class body with the keyword static or prefixing the function declaration with the keyword static. A static function cannot access any other member variables of the object. One does not need to create a class object for accessing a static member or a static function; a static function can be invoked using the class name as class_name::static_member_function.
e.g.
class ShoppingCart
{
private:
static int totalItems; //static data member
public:
static int getTotalItems (); //static member function
};
Related Articles
- What is ‘this’ Pointer (object) in C++
- What are Virtual Functions ? Why do we need Virtual Functions? Explain with an example.
- What are Friend Functions and Friend Classes in C++ ?
- What are classes ? How are they defined ?
- State all the class access modifiers and explain each one of them


