How to use static method and static variable in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

class CClass {
	static int i;
public:
	static void init(int n) {
		i = n;
	}
	void print() {
		cout << i << endl;
	}
};

int CClass::i;

int main()
{
	CClass::init(873);

	CClass o;
	o.print();

	return 0;
}



/*
run:

873

*/

 



answered Aug 2, 2018 by avibootz

Related questions

1 answer 140 views
1 answer 216 views
1 answer 109 views
2 answers 164 views
1 answer 105 views
1 answer 199 views
...