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

W]In c++, write a code segment. It does not need to be a complete code. Define a

ID: 3695776 • Letter: W

Question

W]In c++, write a code segment. It does not need to be a complete code.

Define a class for a type called Counter. An object of this type is used to count things, recording a count that is a non-negative whole number. Include a mutator function that sets the counter to a count given as an 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.

Explanation / Answer

#include <iostream>

class Counter {

public:
static unsigned int count;
int number;
void incrementCount()
{
count++;
}
  
void decrementCount()
{
if(count >0)
{
count--;
}
}
  
unsigned int getCount()
{
return count;
}

void printCount()
{
std::cout<<" Value of count ::"<<count<<std::endl;
}
  
void addCount(unsigned int val)
{
count = count +val;
}

};

unsigned int Counter::count = 0;


int main()
{
Counter c1;
unsigned int gcount = c1.getCount();
std::cout<<" Getcount :::"<<gcount<<std::endl;
c1.incrementCount();
c1.printCount();
c1.decrementCount();
c1.printCount();
c1.addCount(5);

c1.printCount();

return 0;
}