How to declare and use static variables in C++

1 Answer

0 votes
#include <iostream>

using namespace std;

class calc
{
	public:
		int f(void);
};
int calc::f(void)
{
	static int number = 0;

	number++;

	return number;
}

int main()
{
	calc c;
	
	c.f();
	c.f();
	int n = c.f();

	cout << n << endl;

	return 0;
}


/*
run:

3

*/

 



answered Feb 23, 2016 by avibootz
edited Feb 23, 2016 by avibootz

Related questions

1 answer 308 views
1 answer 288 views
288 views asked Feb 23, 2016 by avibootz
1 answer 293 views
1 answer 292 views
1 answer 247 views
1 answer 275 views
1 answer 297 views
...