192k views
5 votes
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.

User TommyN
by
7.4k points

1 Answer

3 votes

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





User Elekwent
by
8.5k points