In C++ programming, Design and code a class to represent this \'little red count
ID: 3841411 • Letter: I
Question
In C++ programming,
Design and code a class to represent this 'little red counter' concept. Your Counter should be able to hold any valid 4-digit number from 0 to 9999. When the 'hundredths' button is clicked for a Counter object that represents 9999, it should wrap to 0 and remember that it has 'overflowed'.
Methods should be provided for:
construction from nothing (setting the counter's value to 0), a certain starting value (which must be in [0..9999] or we'll have failed), or another Counter object
access of the entire Counter, access of the 'dollars', and access for the 'cents' should be provided for the programmer's convenience
mutation at an individual digit level (like the original device with its buttons) as well as to change the counter to an entirely new value (within [0..9999], as during construction)
Also allow the programmer using the class to detect an overflow condition and to reset an overflow condition. (They don't need to be able to set such a state, however.) In fact, you can treat this like cin's fail flag — don't tell 'em 'til they ask. (Programmers call this a polled or asynchronous state — as opposed to an interrupt/synchronous setup.)
But why restrict ourselves to mimicking a tired old plastic device? Let's make it more general by allowing the programmer using the class to change the maximum for the Counter. This can be done as a number of digits or as a specific value — at your discretion. (The last two [base ten] digits are always to be considered the 'cents'.)
Provide the ability to display the Counter's amount in either a monetary fashion (1234 displays as $12.34; or 509 displays as $5.09) or a raw fashion (1234 displays as 1234; or 509 displays as 0509). (As above, the last two [base ten] digits are always considered the 'cents' in the monetary display.)
You do not need to provide any means of direct input from the user. The button interface of the original device is a bit complicated for us to mimic within a generic Counter class. Allow for the application to provide such an interface — or whatever they wish — instead of us.
Place your counter ADT in a library.
To make sure the Counter works correctly, write a driver (test application). Here you can provide any sort of interface you like, but a simple one would be A — 10's, S — 1's, D — 0.1's, and F — 0.01's. (Note that ASDF is right there on the keyboard together... And if you don't believe in the power of ASDF, may I suggest you check out the cul...er...organization. And, no, the .org and the .net are not the same; although the .org may be useful...somehow...)
Explanation / Answer
main.cpp
#include <iostream>
#include "Counter.h"
using namespace std;
//Defining method definitions
//Starting constructors
Counter::Counter(){
count = 0;
a = 0;
s = 0;
d = 0;
f = 0;
}
void Counter::reset(){
count = 0;
}
void Counter::incr1(){
count += 1;
}
void Counter::incr10(){
count += 10;
}
void Counter::incr100(){
count += 100;
}
void Counter::incr1000(){
count += 1000;
}
bool Counter::overflow(){
if (count > MAX_COUNT){
value = true;
}
else{
value = false;
return value;
}
}
void Counter::in_cents(){
a += 1;
incr1();
if(a > 9){
a = 1;
count -= 9;
}
}
void Counter::in_dimes(){
s += 1;
incr10();
if(s > 9){
s = 1;
count -= 90;
}
}
void Counter::in_dollars(){
d += 1;
incr100();
if(d > 9){
d = 1;
count -= 900;
}
}
void Counter::in_10_dollars(){
f += 1;
incr1000();
if(f > 9){
f = 1;
count -= 9000;
}
}
void Counter :: Display(){
if (count != 10000){
cout << "The Count is: " << count << endl;
cout << "The Amount of Cent is: " << a << endl;
cout << "The Amount of Dime is: " << s << endl;
cout << "The Amount of Dollar is: " << d << endl;
cout << "The Amount of 10 Dollars is: " << f << endl;
}
}
void Counter::request_overflow(){
count = 10000;
value = overflow();
if(value == 1){
cout << "Count exceeds Maximum 9999" << endl;
//exit(0);
}
}
void Counter::enter(char p){
if(p =='a'){
in_cents();
}
else if(p == 's'){
in_dimes();
}
else if(p == 'd'){
in_dollars();
}
else if(p == 'f'){
in_10_dollars();
}
else if(p == 'o'){
request_overflow();
}
else{
cout << "Please Re-Enter " << endl;
}
}
int main(){
char input;
Counter C;
cout<<"Please enter the digits (a = 1, s = 10, d = 100, f = 1000, o = overflow): "<<endl;
do{
cin >> input;
C.enter(input);
C.Display();
}while (input != 'o');
cout<<"Overflow Requested. Exiting ..." << endl;
return 0;
}
Counter.h
//Define the class that includes all the actions for counters
//Define the maximum constant that will be used for counting for the rest of the program
const int MAX_COUNT = 9999;
class Counter{
private:
int count;
bool value;
int a;
int s;
int d;
int f;
public:
Counter();
void reset();
void incr1();
void incr10();
void incr100();
void incr1000();
//Define overflow actions
bool overflow();
//These functions limit the numbers between 1-9
void in_cents();
void in_dimes();
void in_dollars();
void in_10_dollars();
//Display values
void Display();
void request_overflow();
void enter(char);
};