Ex3.4: Static Class Counter
This commit is contained in:
parent
2ecd14cf94
commit
7924ee2a62
|
@ -0,0 +1,9 @@
|
||||||
|
// Counter.cc
|
||||||
|
|
||||||
|
#include "Counter.hh"
|
||||||
|
|
||||||
|
int Counter::getCounter() {
|
||||||
|
return counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Counter::counter = 0 ;
|
|
@ -0,0 +1,18 @@
|
||||||
|
// Counter.hh
|
||||||
|
|
||||||
|
#ifndef COUNTERHH
|
||||||
|
#define COUNTERHH
|
||||||
|
|
||||||
|
class Counter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Counter(){ counter++ ; }
|
||||||
|
~Counter(){ counter-- ; }
|
||||||
|
|
||||||
|
static int getCounter();
|
||||||
|
|
||||||
|
|
||||||
|
static int counter ;
|
||||||
|
|
||||||
|
};
|
||||||
|
#endif
|
|
@ -0,0 +1,19 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include "Counter.hh"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Counter a ;
|
||||||
|
Counter b ;
|
||||||
|
|
||||||
|
cout << "there are now "
|
||||||
|
<< Counter::getCounter()
|
||||||
|
<< " Counter objects" << endl;
|
||||||
|
|
||||||
|
if (true) {
|
||||||
|
Counter c;
|
||||||
|
cout << " and now " << Counter::getCounter() ;
|
||||||
|
}
|
||||||
|
cout << " and now " << Counter::getCounter() << endl ;
|
||||||
|
}
|
Reference in New Issue