C++
Static data member in C++
1. Static data
member
Ø A data
member of a class can be static.
Ø Static member
variable is similar to c static variable.
Ø It is
initialized to zero when the first object of its class is created.
Ø No other
initialization is permitted.
Ø Only one
copy of that member is created for entire class and is shared by all the
objects of that class no matter how many objects are created.
Ø 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.
Ø We can define
static variable outside the class also.
Ø Static data
members are stored separately rather than as a [art of an object.
Ø They are
also known as class variable.
Ø While defining
a static variable, some initial value can also be assigned to the variables.
Example of
static data member
#include<iostream>
using namespace std;
class A
{
public:
A()
{cout<<"A's constructor called" <<endl;}
};
class B
{
static A a;
public:
B()
{cout <<"B's constructor called" <<endl;}
};
int main()
{
B b;
return
0;
}

Post a Comment
0 Comments