Ask Question
24 October, 10:28

Write the definition of a class Counter containing: An instance variable named counter of type int. An instance variable named counterID of type int. A static int variable nCounters which is initialized to zero. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the result to the instance variable counterID. A method named increment. It does not take parameters or return a value; it just adds one to the instance variable counter. A method named decrement that also doesn't take parameters or return a value; it just subtracts one from the counter. A method named getValue. It returns the value of the instance variable counter. A method named getCounterID: it returns the value of the instance variable counterID.

+1
Answers (1)
  1. 24 October, 11:18
    0
    The definition of a class Counter

    class Counter

    {

    Private : / /access modifier

    int counter; / /defining and initialising variables

    int counterID;

    static int nCounters=0;

    Public : / /access modifier

    Counter (int a) / /defining functions

    {

    counter=a; / /initialising variable counter with argument a

    nCounters++; / /incrementing 1 to variable nCounters

    }

    void increment ()

    {

    counter=counter + 1;

    }

    void decrement ()

    {

    counter=counter - 1;

    }

    int getValue ()

    {

    return counter; / /returning integer value contained in counter

    }

    int getCounterID ()

    {

    return counterID; / /returning integer value contained in counter

    }

    }; / /class definition ends
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write the definition of a class Counter containing: An instance variable named counter of type int. An instance variable named counterID of ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers