Define a class for a type called CounterType. An object of this type is used to count things, so it records a count that is a nonnegative whole number.
Include a default constructor that sets the counter to zero and a constructor with one argument that sets the counter to the value specified by its argument. Include member functions to increase the count by one and to decrease the count by one. Be sure that no member function allows the value of the counter to become negative. Also, include a member function that returns the current count value and one that outputs the count to a stream. The member function for doing output will have one formal parameter of type ostream for the output stream that receives the output. Embed your class definition in a test program.
#include <iostream>
using namespace std;
/**
class definition for CounterType
*/
class CounterType {
private:
// private variables
int count;
int counter = 0;
public:
// default constructor
CounterType() { count = 0; }
// constructor which accept count value and
// set the value to count
CounterType(int c) { count = c; }
// incrementing counter
void increseCounter() { counter++; }
// decreasing the count
int decreaseCount() {
// if count reach to then returning false
if (count == 0)
return 0;
count--;
return 1;
}
// methods for get count and get counter
int getCount() { return count; }
int getCounter() { return counter; }
// method for printing the info
void print_c(ostream &cout) {
cout << "Count : " << getCount() << "\n";
cout << "Counter : " << getCounter() << "\n\n";
}
};
// test method
int main(int argc, char const *argv[]) {
int value;
cout << "Please enter a COUNT value: ";
cin >> value;
CounterType c(value);
// using while loop printing
// count and counter
while (1) {
c.print_c(cout);
c.increseCounter();
int t = c.decreaseCount();
// if count reach to 0 then nothing program will exit
if (!t) {
break;
}
}
return 0;
}// end of program