Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define a class for a type called CounterType. An object of this type is used to

ID: 3678782 • Letter: D

Question

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 the member variable(s) you think are appropriate for this class. Also, include the following member functions:

a) a default constructor that sets the counter to zero

b) a constructor with one argument that sets the counter to the value specified by its argument

c) a function used to increase the count by one

d) a function used to decrease the count by one

e) a function that returns (get function) the current count value

f) a function for that outputs the count to a stream. This output function should have one formal parameter of type ostream for the output stream that receives the output (note that this can be cout or an output file).

Be sure that no member functions allow the value of the counter to become negative (Provide appropriate messaging in case the value becomes zero). Embed your class definition in a test program.

Explanation / Answer

Hi i have written the code below for your reference,

#include <iostream>

#include <cstdlib> #include <string> using namespace std; // Function Declaration void output(ostream&, string); class CounterType { public: // Set up the class. CounterType(int); CounterType(); // API Function. void output(ostream&); int get_counter(); void add(); void remove(); private: int counter; bool check_amount(int); }; // Set up the template of CounterType with the given amount. // When a negative number kill program. CounterType::CounterType(int amount) { if (amount < 0) { cout << "NOTICE: Incorrect amount input defaulting to 0." << endl; counter = 0; return; } counter = amount; } // Set up the template of CounterType with default of 0. CounterType::CounterType() { counter = 0; } // Output the current amount to the given out stream. void CounterType::output(ostream& stream) { stream << "Counter is at: "; stream << get_counter() << endl; } // @return the current amount of counter. int CounterType::get_counter() { return counter; } // Add one to the counter. void CounterType::add() { int count = get_counter(); // Should never happen but add it anyways. if (check_amount(count + 1)) { counter = count + 1; } } // Remove one from the counter. void CounterType::remove() { int count = get_counter(); if (check_amount(count - 1)) { counter = count - 1; } } // Check if the amount will make the counter zero. bool CounterType::check_amount(int amount) { if (amount < 0) { cout << "WARNING: New amount will be negative. " << "Will not change counter." << endl; return false; } return true; } // Main Function int main() { // Count Setup cout << "Counter One: Setup" << endl; CounterType counter_one; cout << "Counter Two: Setup" << endl; CounterType counter_two(10); cout << "Counter Three: Setup" << endl; CounterType counter_three(-10); cout << endl; // Counter Amount. cout << "Counter One: Amount" << endl; counter_one.output(cout); cout << "Counter Two: Amount" << endl; counter_two.output(cout); cout << "Counter Three: Amount" << endl; counter_three.output(cout); cout << endl; // Counter Remove One. cout << "Counter One: Remove One" << endl; counter_one.remove(); cout << "Counter Two: Remove One" << endl; counter_two.remove(); cout << "Counter Three: Remove One" << endl; counter_three.remove(); cout << endl; // Counter Amount. cout << "Counter One: Amount" << endl; counter_one.output(cout); cout << "Counter Two: Amount" << endl; counter_two.output(cout); cout << "Counter Three: Amount" << endl; counter_three.output(cout); cout << endl; // Counter Add One. cout << "Counter One: Add One" << endl; counter_one.add(); cout << "Counter Two: Add One" << endl; counter_two.add(); cout << "Counter Three: Add One" << endl; counter_three.add(); cout << endl; // Counter Amount. cout << "Counter One: Amount" << endl; counter_one.output(cout); cout << "Counter Two: Amount" << endl; counter_two.output(cout); cout << "Counter Three: Amount" << endl; counter_three.output(cout); cout << endl; return 0; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote