Ex3.4: Static Class Counter
This commit is contained in:
parent
2ecd14cf94
commit
7924ee2a62
3 changed files with 46 additions and 0 deletions
9
ex3.4/Counter.cc
Normal file
9
ex3.4/Counter.cc
Normal 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
18
ex3.4/Counter.hh
Normal 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
19
ex3.4/main.cpp
Normal 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 ;
|
||||
}
|
Reference in a new issue