Static elements are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime. Static Keyword can be used with following,
1. Static variable in functions
2. Static Class Objects
3. Static member Variable in class
4. Static Methods in class
Static variables inside Functions
Static variables when used inside function are initialized only once, and then they hold there value even through function calls.These static variables are stored on static storage area, not in stack.
Example :
#include<iostream.h>
#include<conio.h>
void fun()
{
staticint a;
cout<<"The a value is "<<a<<endl;
a++;
}
void fun1()
{
int a=1;
cout<<"The a value is "<<a<<endl;
a++;
}
void main()
{
clrscr();
fun();
fun();
fun1();
fun1();
getch();
}
If we do not use static keyword, the variable count, is reinitialized everytime when fun1() function is called, and gets destroyed each time when fun1() functions ends. But, if we make it static, once initialized count will have a scope till the end of main() function and it will carry its value through function calls too.If you don't initialize a static variable, they are by default initialized to zero.
Static class Objects
Static keyword works in the same way for class objects too. Objects declared static are allocated storage in static storage area, and have scope till the end of program.Static objects are also initialized using constructors like other normal objects. Assignment to zero, on using static keyword is only for primitive datatypes, not for user defined datatypes.
Example :
#include<iostream.h>
#include<conio.h>
class sa
{
public:
sa()
{
cout<<"This is Default Constructor"<<endl;
}
void print()
{
cout<<"Welcome";
}
~sa()
{
cout<<"This is Destructor";
}
};
void main()
{
clrscr();
static sa s;
s.print();
getch();
}
Static data member in class
Static data members of class are those members which are shared by all the objects. Static data member has a single piece of storage, and is not available as separate copy with each object, like other non-static data members.Static member variables (data members) are not initialized using constructor, because these are not dependent on object initialization.Also, it must be initialized explicitly, always outside the class. If not initialized, Linker will give error.
Example :
#include<iostream.h>
#include<conio.h>
class sa
{
public:
static int c;
public:
void print()
{
cout<<"The static variable value is "<<c<<endl;
c++;
}
};
intsa::c=10;
void main()
{
clrscr();
sa s;
s.print();
cout<<"The static variable value is "<<s.c;
cout<<"The static variable value is "<<sa::c;
getch();
}
Once the definition for static data member is made, user cannot redefine it. Though, arithmetic operations can be performed on it.
Static Member Functions
These functions work for the class as whole rather than for a particular object of a class.It can be called using an object and the direct member access .operator. But, its more typical to call a static member function by itself, using class name and scope resolution :: operator.
Example :
#include<iostream.h>
#include<conio.h>
classsa
{
public:
staticint c;
int a;
public:
void read()
{
cout<<"Enter two values";
cin>>a>>c;
}
static void inc()
{
c++;
// a++;
}
void print()
{
cout<<"The static variable value is "<<c<<endl;
cout<<"The variable value is "<<a<<endl;
}
};
intsa::c;
void main()
{
clrscr();
sa s;
s.read();
s.print();
sa::inc();
cout<<"The static variable value is "<<sa::c<<endl;
cout<<"The variable value is "<<s.a<<endl;
s.inc();
cout<<"The static variable value is "<<s.c<<endl;
cout<<"The variable value is "<<s.a<<endl;
getch();
}
1. Static variable in functions
2. Static Class Objects
3. Static member Variable in class
4. Static Methods in class
Static variables inside Functions
Static variables when used inside function are initialized only once, and then they hold there value even through function calls.These static variables are stored on static storage area, not in stack.
Example :
#include<iostream.h>
#include<conio.h>
void fun()
{
staticint a;
cout<<"The a value is "<<a<<endl;
a++;
}
void fun1()
{
int a=1;
cout<<"The a value is "<<a<<endl;
a++;
}
void main()
{
clrscr();
fun();
fun();
fun1();
fun1();
getch();
}
If we do not use static keyword, the variable count, is reinitialized everytime when fun1() function is called, and gets destroyed each time when fun1() functions ends. But, if we make it static, once initialized count will have a scope till the end of main() function and it will carry its value through function calls too.If you don't initialize a static variable, they are by default initialized to zero.
Static class Objects
Static keyword works in the same way for class objects too. Objects declared static are allocated storage in static storage area, and have scope till the end of program.Static objects are also initialized using constructors like other normal objects. Assignment to zero, on using static keyword is only for primitive datatypes, not for user defined datatypes.
Example :
#include<iostream.h>
#include<conio.h>
class sa
{
public:
sa()
{
cout<<"This is Default Constructor"<<endl;
}
void print()
{
cout<<"Welcome";
}
~sa()
{
cout<<"This is Destructor";
}
};
void main()
{
clrscr();
static sa s;
s.print();
getch();
}
Static data member in class
Static data members of class are those members which are shared by all the objects. Static data member has a single piece of storage, and is not available as separate copy with each object, like other non-static data members.Static member variables (data members) are not initialized using constructor, because these are not dependent on object initialization.Also, it must be initialized explicitly, always outside the class. If not initialized, Linker will give error.
Example :
#include<iostream.h>
#include<conio.h>
class sa
{
public:
static int c;
public:
void print()
{
cout<<"The static variable value is "<<c<<endl;
c++;
}
};
intsa::c=10;
void main()
{
clrscr();
sa s;
s.print();
cout<<"The static variable value is "<<s.c;
cout<<"The static variable value is "<<sa::c;
getch();
}
Once the definition for static data member is made, user cannot redefine it. Though, arithmetic operations can be performed on it.
Static Member Functions
These functions work for the class as whole rather than for a particular object of a class.It can be called using an object and the direct member access .operator. But, its more typical to call a static member function by itself, using class name and scope resolution :: operator.
Example :
#include<iostream.h>
#include<conio.h>
classsa
{
public:
staticint c;
int a;
public:
void read()
{
cout<<"Enter two values";
cin>>a>>c;
}
static void inc()
{
c++;
// a++;
}
void print()
{
cout<<"The static variable value is "<<c<<endl;
cout<<"The variable value is "<<a<<endl;
}
};
intsa::c;
void main()
{
clrscr();
sa s;
s.read();
s.print();
sa::inc();
cout<<"The static variable value is "<<sa::c<<endl;
cout<<"The variable value is "<<s.a<<endl;
s.inc();
cout<<"The static variable value is "<<s.c<<endl;
cout<<"The variable value is "<<s.a<<endl;
getch();
}
These functions cannot access ordinary data members and member functions, but only static data members and static member functions.