Answer:
File: Counter.h
// Counter class specification
#ifndef COUNTER_H
#define COUNTER_H
#include <iostream>
#include <string>
using namespace st d;
class Counter
{
private:
// A data member counter of type int
int counter;
// A data member named limit of type int
int limit;
// A static int data member named nCounters
static int nCounters;
public:
// A constructor that takes two int arguments.
Counter(int aCounter, int aLimit);
// A function called increment that accepts
// no parameters and returns no value.
void increment();
// A function called decrement that accepts
// no parameters and returns no value.
void decrement();
// A function called getValue that accepts
// no parameters and returns an int.
int getValue();
// A static function named getNCounters that
// accepts no parameters and returns an int.
static int getNCounters();
};
#endif
File: Counter.cpp
// Counter class specification
#include "Counter.h"
// initializ the static int data member nCounters to 0
int Counter::nCounters = 0;
// The constructor takes two int arguments and assigns
// the first one to counter and the second one to limit.
// It also adds one to the static variable nCounters.
Counter::Counter(int aCounter, int aLimit)
{
counter = aCounter;
limit = aLimit;
nCounters++;
}
// The increment function accepts no parameters and
// returns no value. It adds one to the instance variable
// counter if the data member counter is less than limit.
void Counter::increment()
{
if (counter < limit)
{
counter++;
}
}
// The decrement function accepts no parameters and
// returns no value. It subtracts one from the counter
// if the data member counter is greater than zero.
void Counter::decrement()
{
if (counter > 0)
{
counter--;
}
}
// The getValue function accepts no parameters and returns
// the value of the instance variable counter.
int Counter::getValue()
{
return counter;
}
// The getNCounters function accepts no parameters and
// returns the value of the static variable nCounters.
int Counter :: getNCounters()
{
return nCounters;
}
File: main.cpp
// test program for Counter class
#include "Counter. h"
// start main function
int main()
{
// create two objects for Counter class
Counter c1(2, 5);
Counter c2(7, 10);
// print the initial values of the data member counter
cou t << "\\New value of c1: " << c1.getValue() << endl;
cou t << "New value of c2: " << c2.getValue() << endl;
/ print the value of the static variable nCounters of Counter class
cou t << "\\Number of Counters: " << Counter::getNCounters() << endl;
return 0;
} // end of main function
Explanation:
Sample output
Initial value of C1: 2
Initial value of C2: 7
New value of C1: 3
New value of C2: 6
Number of counters: 2