What do you mean by static class members?
What do you mean by static class members? Explain
static class members
Data member and member functions of a class in C++ may be qualified as static. One can have static data members and static member functions in a class. Thus a class contain two types of static members
static members
i) Data member
ii) Member function
Static data member
A static data member has a property that all instances of the containing class share this one data member.
-
To declare it within the class
-
To define data outside the class.
Static member variables belong to the class, not to the object. They can be accessed from outside the class with the scope resolution operator.
Definition of static data member is:
return_type class name :: static_data_membername;
Static data members are used when the information is to be shared. They can be public or private data. They should be created and initialized before the main() function.
For example:
class counter
{
static int count; // count is defined as static
.............................
.............................
};
The static data member is defined outside the class as:
int counter :: count; // definition outside class
The definition outside the class is a must. One can also initialize the static data member at the time of the definition as:
int counter :: count=0;
If one defines three objects as:
counter obj1, obj2, obj3;
Features of static data members
Static data member has the following features:
-
Only a single copy of the static data member is used by all the objects.
-
By default, a static data member is initialized to zero when the first object of its class is created.
-
The access rule of static. The data member is the same as the other data members of the class. For example, when a static data member is declared private, the non-member functions can not access this member.
-
It can be used within the class but its lifetime is the whole program.
Program example:
#include<iostream.h>
#include<conio.h>
class counter
{
static int count;
public:
void display()
{
++count;
count<<”value of count=”<<count<<endl;
}
};
int counter :: count;
void main()
{
counter obj1, obj2, obj3;
clrscr();
obj1.display();
obj2.display();
obj3.display();
getch();
}
output
value of count = 1
value of count = 2
value of count = 3
From the program, it's clear that the data member count is initialized to 0 by default when the object count is created. The static data member is common to all the three objects obj1, obj2, and obj3.
Static member function
The declaration of a static member function is the same as that of a nonstatic member function except that the function declaration in the class body is preceded by the keyword static. The function definition that appears outside of the class body must not specify the keyword static.
A static member function can access only the static members of a class. It is called by using the name of the class rather than an object
name_of_the_class:: function_name
Program example:-
#include<iostream.h>
#include<conio.h>
class test
{
static int x;
public:
static void getdata(int a)
{
x=a;
}
void show()
{
cout<<x;
}
};
int test :: x;
void main()
{
clrscr();
test :: getdata(50);
test x;
x.show();
getch();
}
output is:
50
In this program, the static member function getdata() has been invoked prior to the creation of the object x, the getdata() static member function initializes the static data member x. So the static data member x has been initialized prior to the creation of any object.