Static Keyword
Static Keyword:
Static keyword is use like follow:
datatype classname
- Element store with keyword Static
- Static element never change during program
- Scope of the static member is same as lifetime of program
Static keyword is use like follow:
- Static variable in functions
- Static Class Objects
- Static member Variable in class
- Static Methods in class
Syntax:
functionname()
{
..............
static variablename();
..............
}
Static Class Objects
Syntax:
datatype classname
{
.............
.............
static classname objectname;
.............
.............
};
Static member Variable in class
Syntax:
datatype classname
{
...............
...............
static datatype variablename;
..............
..............
};
Static Methods in class
Syntax:datatype classname
{
..............
..............
static datatype methodname();
.............
};
Use of a static member
Function.
CODE:
#include <iostream>
using namespace std;
class test
{
{
int code;
static int count;
public:
void setcode(void)
{
code = ++count;
}
void showcode(void)
{
cout << "object number :" << code << "\n";
}
static void showcount(void)
{
cout << "count:" << count << "\n";
}
};
int test::count;
int main()
{
cout << "W.A.P. that illustrates the use of a static member function.\n\n\n";
cout << "W.A.P. that illustrates the use of a static member function.\n\n\n";
test t1, t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}
OUTPUT:
0 Comments