1
0
Fork 0

Ex3.4: Static Class Counter

This commit is contained in:
Eric Teunis de Boone 2019-12-12 17:12:47 +01:00
parent 2ecd14cf94
commit 7924ee2a62
3 changed files with 46 additions and 0 deletions

9
ex3.4/Counter.cc Normal file
View File

@ -0,0 +1,9 @@
// Counter.cc
#include "Counter.hh"
int Counter::getCounter() {
return counter;
}
int Counter::counter = 0 ;

18
ex3.4/Counter.hh Normal file
View File

@ -0,0 +1,18 @@
// Counter.hh
#ifndef COUNTERHH
#define COUNTERHH
class Counter
{
public:
Counter(){ counter++ ; }
~Counter(){ counter-- ; }
static int getCounter();
static int counter ;
};
#endif

19
ex3.4/main.cpp Normal file
View File

@ -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 ;
}