Static variable in C++ by R4R Team

Static variable:
Static variable are those variable whose memory is shareable.
or,
Static variable are declared once in entire life time of the program.

Synatx:
static data_type variable_name;

program:

#include < iostream>

using namespace std;
//class declared
class Myclass
{
public:
void function()
{
//line 1
static int a=10;
cout<< a<< endl;
a=a+5;
}
};
int main()
{
Myclass obj;
obj.function();
obj.function();
return 0;
}


output-

10
15

-In this program, we call function() 2 times but we get different output because in second time line 1 is not executed.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!