In this article static data member we give the information about instance data members are variables that are created separately for each object, but Static Data Member are such variable, are common to all objects of that class and are shared by all objects.

Static Data Member in C++:

Instance Data Members are variables that are created separately for each object, but Static Data Members are such variables, are common to all objects of that class and are shared by all objects. Therefore, if any object of the class changes the value of that, then the value of that variable also changes for all other objects of that class.

Whereas when an Object changes the value of its Instance variable, that change does not affect the Instance that of any other Object, because even all Objects of the same class have their own Separate Instance variable, which is Static. Like a variable, it is not shared between all the objects of the class.

When we have to declare a variable in a class as static, then we have to do this work in a slightly different way. We have to define the data member outside the class by declaring the static variable inside the class.

Syntax:

Class_class name

{

Data type

Static variable

…………………….

…………………….

};

Data type class name :: variable initialization

Example:

class reverse

{

private:

static int number;   // declaration

};

int reverse::number = 10;   // definition

At the time of Declaration of Variable, we have to use the static keyword in the Class and while defining the Variable, we have to add the name of that Class with the name of the Variable using the Scope Resolution Operator, in which the Class in which that Static Variable was declared.

When we define a variable inside a class, then that variable is always the instance data of the object which is different for every object of that class. But when we define a data item outside a class then that data item is the same for all the objects of the class which all the objects of the class can share equally.

  • While defining a Static Data Member, it can also be initialized.
  • If we do not initialize any static data member, then automatically Zero(0) gets initialized.
  • Once a Static Data Member is defined, that Data Member can be accessed only by the Member Function of the class in which the Static Data Member is defined.
  • Scope of such variable is up to Class.

Example

int A :: variable = 20;

This definition initializes the variable to 20.

Static Data Members Program:-

// static data member
#include<iostream.h>
#include<conio.h>
class item
{
int num; // auto variable
static int count; // variable declaration
public:
void getdata(int a) // mumber function
{
num=a;
count++;
cout<<“\n\n Value of Num: ” <<num;
}
void display_Count()
{
cout<<“\n\n Value of Count: “<<count;

}
};
int item::count=10; // Definition
void main()
{
item p,q,r; // objets created
clrscr();
p.display_Count(); // function call
q.display_Count();
r.display_Count();
p.getdata(100);
q.getdata(200);
r.getdata(300);
p.display_Count(); // function call
q.display_Count();
r.display_Count();
getch();
}

OUTPUT:-

Value of Count: 10

                       : 10

                       : 10

Value of Num: 100

                      : 200

                      : 300

Value of Count: 13

                       : 13

                       : 13

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *