Browse Source

Ex3.4: Static Class Counter

master
Eric Teunis de Boone 2 years ago
parent
commit
7924ee2a62
  1. 9
      ex3.4/Counter.cc
  2. 18
      ex3.4/Counter.hh
  3. 19
      ex3.4/main.cpp

9
ex3.4/Counter.cc

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

18
ex3.4/Counter.hh

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

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