199k views
1 vote
My programming lab 9.2 C++ Write a full class definition for a class named Counter, and containing the following members:_______.

A) A data member counter of type int.
B) A constructor that takes one int argument and assigns its value to counter
C) A function called increment that accepts no parameters and returns no value.
D) Increment adds one to the counter data member.
E) A function called decrement that accepts no parameters and returns no value.
F) Decrement subtracts one from the counter data member.
G) A function called getValue that accepts no parameters.
H) It returns the value of the instance variable counter.

User Rouble
by
6.2k points

1 Answer

5 votes

Final answer:

The Counter class definition in C++ includes a private integer data member, a constructor, increment and decrement functions, and a getValue function to manage and retrieve the counter's value.

Step-by-step explanation:

To write a full class definition for a class named Counter, which includes various members such as a data member, a constructor, and functions to increment, decrement, and get the value of the counter, the following C++ code can be used:

class Counter {
private:
int counter;

public:
// B) Constructor that initializes the counter to a given value
Counter(int initialCount) : counter(initialCount) {}

// C) & D) Increment function to add one to the counter
void increment() {
counter++;
}

// E) & F) Decrement function to subtract one from the counter
void decrement() {
counter--;
}

// G) & H) Function to get the current value of the counter
int getValue() const {
return counter;
}
};

This class consists of an int type data member named counter, a constructor that initializes this data member, an increment function which increases the counter by one each time it's called, a decrement function which does the opposite by decreasing the counter by one, and a getValue function that returns the current value of the counter. These members encapsulate the main functionalities expected from a counter.

User Anto
by
5.7k points