Static data member in c++ with example program

P.Priyadharshini | 2595 Views | c++ | 08 Aug 2016

 

What is a static data members in c++ :

  • Static data members are not part of objects of a given class type. As a result, the declaration of a static data member is not considered a definition. The data member is declared in class scope, but definition is performed at file scope. These static members have external linkage.
  • The declaration of a static data member in the member list of a class is not a definition. You must define the static member outside of the class declaration, in namespace scope.
  • A data member of a class can be qualified as static. The properties of a static member variable are similar to that of a C static variable. A static member variable has certain special characteristics. These are:

1. It is initialized to zero when the first object of its class is created. No other initialization is permitted.

2. Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.

For example :

class X
{
public:
      static int i;
};
int X::i = 0; // definition outside class declaration
  • Once you define a static data member, it exists even though no objects of the static data member's class exist. In the above example, no objects of class X exist even though the static data member X::i has been defined.

Static Keyword

  • Static is a keyword in C++ used to give special characteristics to an element. Static elements are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime. Static Keyword can be used with following,

1. Static variable in functions

2. Static Class Objects

3. Static member Variable in class

4. Static Methods in class

Static variables inside Functions :

  • Static variables when used inside function are initialized only once, and then they hold there value even through function calls.

Static class Objects :

  • Static keyword works in the same way for class objects too. Objects declared static are allocated storage in static storage area, and have scope till the end of program.

Static member variable in class :

  • Static data members of class are those members which are shared by all the objects. Static data member has a single piece of storage, and is not available as separate copy with each object, like other non-static data members.
  • Static member variables (data members) are not initialed using constructor, because these are not dependent on object initialization.

Static Member in class :

  • These functions work for the class as whole rather than for a particular object of a class.
  • It can be called using an object and the direct member access . operator. But, its more typical to call a static member function by itself, using class name and scope resolution :: operator.

Static data members program in c++ :

#include<iostream.h>
#include<conio.h>
 class staticmember
  {
    int code;
    static int count;
   public:
    staticmember()
      {
        code=++count;
      }
    void showcode()
      {
        cout<<"\n\tMy id is :"<<code;
      }
    static void showcount()
      {
        cout<<"\n\tCount Objects :"<<count;
      }
};
int  staticmember::count;
void main()
{
   clrscr();
   stat obj1,obj2;
   obj1.showcount();
   obj1.showcode();
   obj2.showcount();
   obj2.showcode();
   getch();
}

Output :

Count Objects: 2
My id is: 1
Count Objects: 2
My id is: 2

Difference between static data member and static member function in c++ :

Static Data Members :

  • Static Data Members are those which are declared by using the Static Keyword in front of the Data Members. Means Put the Static Keyword in front of the Variables. And Static Data Members always have Default values as\ 0 for int and Null for Strings. So that they will Never Stores any Garbage values. Always remember that Static Data Members are always used in the Static Member Functions. Means if a Member Functions wants to use a Static Data then we must have to declare that Member Function as Static. And the Static Data Members are always Assigned Some values from the outside from the Class.

Static Member Functions :

  • The Static Member Functions are those which are declared by using the Static in Front of the Member Function. And in this we use the Static Data Members.  Any Method can be converted into the Static just by Using the Static in front of the Member Function. For Accessing the Static data Member Function we doesn’t need to Create an Object of Class and we will call the Function with the name of Class and Scope resolution Operator.  So that there will be no object and no Extra Memory is required to Store the Data of a Class.