Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An data member named limit of type int. A static int data member named nCounters which is initialized to 0 (this should be done as part of the implementation, i.e., you need to supply this initialization). A constructor that 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 A function called increment that accepts no parameters and returns no value. If the data member counter is less than limit, increment just adds one to the instance variable counter. A function called decrement that accepts no parameters and returns no value. If counter is greater than zero, decrement subtracts one from the counter. A function called getValue that accepts no parameters. It returns the value of the instance variable counter. A static function named getNCounters that accepts no parameters and return an int. getNCounters returns the value of the static variable nCounters.

Respuesta :

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 << "\nNew 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 << "\nNumber 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