Static Data Members
C++ permits programmers to define static data members within
a class. If a data member is preceded by the static keyword is called static
data member. Irrespective of the number of objects created, only a single copy
of the static member is created in memory.
For a static data member the memory is allocated only once
along with the class definition is loaded, but for a data member, the memory is
allocated for each object.
When a data member is declared as static, the following must be noted.
- All objects of a class share the static members.
- All static data members are initialized to zero when the first object of the class is created.
- Static data members are visible only within the class but their lifetime is the entire program.
- Static data members are usually used to maintain values that are common for the all the objects
- Static data members are declared inside a class, they are not considered to be a part of the objects.
- A static data member has a file scope.
- A static data member is declared inside the class, they can be accessed only by using the class name and the scope resolution operator.
- A static data member is initialized in outside the class using the class name and scope resolution operator.
Syntax:
class classname
{
private:
static datatype variable;
----------
public:
-------
};
datatype
classname : : variable = value;
Example:
Static Member Functions
C++ not only allows data members of
a class to be static but also supports static member functions. Any static
member; either data or function - is allocated memory only once along with
class definition.
Static member is not a part of any
object, but it is shared by all the objects of that class.
A static member function has the
following features.
•
A static member function can access only the static members (data and/or
functions) declared in the same class.
•
It cannot access non-static members because they belong to an object.
•
A static member is called using the class name and the scope
resolution operator.
•
A static member function is preceded by the keyword “static‟, followed by
the function definition.
Syntax:-
static return_type
fun_name([args_list])
{
-------------
}
A static member function can be
called using the class name, but not with the object. To invoke a static member
function scope resolution operator is used.
Syntax:-
classname :: fun_name(args);
Example:
#include<iostream.h>
#include<conio.h>
class id_generator
{
private:
static int next_id;
public:
static void gen_next_id()
{
cout<<"\nNEXT ID=
"<<next_id++;
}
};
int id_generator::next_id=1;
void main()
{
clrscr();
for(int i=0;i<5;i++)
id_generator::gen_next_id();
getch();
}
Every time the static function is called, it increments
the value of static variable by 1.
In this program we have not made any object
of the class. This is because our class has only one member function and that
is static.
Static member functions are not
invoked by objects. They are called using the class name and scope resolution
operator.
No comments:
Post a Comment