STATIC DATA MEMBERS
Objects of a class keep their own copy of data members of class. In certain situations a part of data may be same for all the objects of a class. In such a cases instead of every object keeping an individual copy of this data, it would be advantageous if one copy of the data is shared by all the objects of class, because, this would save memory space, particularly, when the common data is large. For such applications the common data may be declared as static. The static data may be declared under one of the three access specifiers, i.e. public, protected or private. A public static data member may be accessed directly through any object of the class because it is same for all the objects. Static data member declared private or protected may be accessed through the corresponding public functions of the class.
Characteristics
· Each change affects to all the objects.
· It is automatically initialized with zero when the first object of its class is created. No other initialization is permitted.
· It is visible only within the class, but its lifetime is the entire program.
· Static variables are normally used to maintain values common to the entire class.
Program 7:
#include<iostream.h>
#include<conio.h>
class data
{
int x;
static int y;
public:
void getdata(int a, int b)
{
x=a;
y=b;
}
void show()
{
cout << "\n x= " << x << "\n y = " << y;
}
};
int data :: y;
void main()
{
data d1,d2;
d1.getdata(5,10);
d2.getdata(9,20);
d1.show();
d2.show();
getch();
}
Output:
X= 5 y=20
X=9 y=20
Note: int data :: y; // definition of static data member
Note that the type and scope of each static member variable must be defined outside the class definition. This is necessary because the static data members are stored separately rather than as a part of an object. Since they are associated with the class itself rather than with any class object, they are also known as class variables.
Static variables are like non-linear member functions as they are declared in a class declaration and defined in the source file. While defining a static variable, some initial values can also be assigned to the variable.
int data :: y=10;
STATIC MEMBER FUNCTIONS
A function member of a class may also be declared static provided its arguments are also declared static or if it does not access any non-static member of the class. The pointer this associated with every object of class is applicable only to non-static function members of the class. The static function members of the class do not have this pointer .The main purpose of a static member function is to access and modify static data member of the class.
Properties
· A static function have access to only other static members (functions or variables) declared in the same class.
· A static function can be called using the class name (instead of its objects).
Program 8:
#include<iostream.h>
#include<conio.h>
class interest
{
int p,n;
static float r;
public:
static void set_rate(float q)
{
r=q;
}
void inter()
{
cout << "\n enter the p and n ";
cin >> p >> n;
cout << "the rate of interest = " << (p*r*n);
}
}x;
float interest :: r=4.5;
void main()
{
x.inter();
interest ::set_rate(5.5);
x.inter();
getch();
}
Note: Here we can’t declare rate=4.5 in private because we can’t assign values in private section, so we need to declare “float interest :: r=4.5” outside the class.
Compiled By: Chaudhary Amit V.
Comments
Post a Comment