SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Defining Classes
Exercise:
Programming Projects
Question:5 | ISBN:9780321531346 | Edition: 7

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 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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#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
 

0 0

Discussions

Onlinehelper2020

After executed the above program code, the output result:

Post the discussion to improve the above solution.