What are static Data and Static Member Functions in C++ ?

October 28, 2007 · Filed Under C++ Questions, Placement Questions 



Don't want to miss a single bit? Subscribe By Email for Daily Jobs

Enter your email address :

All the latest from JobsAdda on Jobsadda.com delivered to your mailbox everyday.

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


Get Latest JobsAdda.com Jobs, news & updates  via Email