C++ PROGRAMMING: Define a class for a type called CounterType. An object of this
ID: 3756773 • Letter: C
Question
C++ PROGRAMMING: 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 1 and to decrease the count by 1.
Be sure that no member function allows the value of the counter to become negative.
The count should go down to zero, and the counter should start at 0 and go up by one, at that point it stops.
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.
==============================================
DESIRED OUTPUT BELOW
==============================================
Please enter a COUNT value: 10
Count: 10
Counter: 0
Count: 9
Counter: 1
Count: 8
Counter: 2
Count: 7
Counter: 3
Count: 6
Counter: 4
Count: 5
Counter: 5
Count: 4
Counter: 6
Count: 3
Counter: 7
Count: 2
Counter: 8
Count: 1
Counter: 9
Count: 0
Counter: 10
==============================================
END OF PROGRAM
==============================================
The number 10 was used in this example, the counts and counter should work the same if the number 5, 50, or 500 was input as well.
Explanation / Answer
Please save the below code counterType.cpp or another name with .cpp extension
#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() << " ";
cout << "Counter : " << getCounter() << " ";
}
};
// 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
/*
OUTPUT 1
Please enter a COUNT value: 10
Count : 10
Counter : 0
Count : 9
Counter : 1
Count : 8
Counter : 2
Count : 7
Counter : 3
Count : 6
Counter : 4
Count : 5
Counter : 5
Count : 4
Counter : 6
Count : 3
Counter : 7
Count : 2
Counter : 8
Count : 1
Counter : 9
Count : 0
Counter : 10
OUTPUT 2
Please enter a COUNT value: 15
Count : 15
Counter : 0
Count : 14
Counter : 1
Count : 13
Counter : 2
Count : 12
Counter : 3
Count : 11
Counter : 4
Count : 10
Counter : 5
Count : 9
Counter : 6
Count : 8
Counter : 7
Count : 7
Counter : 8
Count : 6
Counter : 9
Count : 5
Counter : 10
Count : 4
Counter : 11
Count : 3
Counter : 12
Count : 2
Counter : 13
Count : 1
Counter : 14
Count : 0
Counter : 15
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.