Static Data Members

In our last post we have stated that classes contain both data members and member functions.Generally we classify class members either as public or private.There is one more classification of class member as static member.We can have static data member or static member function.Now we shall learn about these static members.

Static Data Members: The keyword to specify a data member as static is “static”. The syntax of declaration of member as static is
static data_type Variable_name;
The main feature of a static data member is, the variable which is declared as static is shared among all objects belonging to same class as data member.Therefore any changes to the variable using any object will be reflected to variable.IF we are accessing the variable with other object the changes done by other object will be displayed.But if consider general declaration of variable it is not shared by all the objects of class.Therefore any change to the variable will not be reflected to the other object.Since the static variable is shared we should define the variable outside the class as that of a member function.Consider an example.

class shared
   int a;
   static int b;         //Declaration of static data member//
int shared::b;          //Declaration of static data member outside class//

Static Data function: Like static data members we can define static member functions.These functions are also shared between objects of the class.The calling of the member functions is different from general calling. These static functions are called using the class name as reference.Let us consider that we have static member function display declared in the class shared.Now the calling of function in the class is as follows:

Void main()
    cout<<”Entered values are:”;
    shared::display();                               //Calling of static member function//       

About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my Master's in Computer Science.
This entry was posted in C++ and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s