Static data
Static data
Explain static
data member? Write the characteristic of it.
Ø
A
class data member can be qualified as static.
Ø
Static
variables are normally used to maintain values common to the entire class.
Ø
For
example: a static data member can be
used as a counter that records the occurrences of all the objects.
Ø
Type and scope of every static member variable must be defined outside the class
definition, because it is stored separately rather than as a part of an object.
Ø
Since
they are associated with the class not an object, they are known as class
variables.
Ø
Example:
class abc
{
static int
count;
};
int abc ::
count;
Ø
The
static variable is initialized to zero when the objects are created.
Ø
Only
one (common) copy of static data member is created and it is shared by all
objects of the class.
Ø
Static
variables are like non-inline member functions as they are declared in a class
declaration and defined in the source file.
Characteristic
of static data member are as follow:
Ø 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 crated.
Ø It is visible
only within the class, but its life is in the entire program.
8)
Explain the
static member functions. Write the characteristics of it.
Syntax:
static
return-type function-name(argument list)
{
//Function body
}
Ø
A
static member function is call directly by the class name, there is no need of an
object to call the static member function.
Ø
To
call a static member function scope resolution operator (::) is used, like
class-name ::
static-function-name();
Characteristic
of static member functions
o A static
function can have access to only other static members (functions or variables)
declared in the same class.
o A static member
function can be called using the class name (instead of its objects) as
follows:
Class-name ::
function-name;
When do we need
to use the static data member in the class?
Ø
When
we want to maintain a value, common to the entire class, a static data member
is used.
Ø
It
is initialized to 0(zero) when a first object of the class is created and
shared among all the objects of that class.
Ø
No
other initialization is permitted with static data members.
Ø
It
is used in such a situation when we want to find how many hits are there on the
class or function.