How to Implement classes?

Hi can you guide me on how to implement member functions of LimitedCounter, OverflowCounter and UseCounter in my code below?

Here's the task:

Derive a class called LimitedCounter.

• The constructor takes two parameters: initial value and upper limit
• Counter can’t be incremented over the upper limit. If inc() is called when counter has reached the upper limit nothing happens
• Counter can’t be decremented below zero. If counter is zero then calling dec() will have no effect


Derive a class called OverflowCounter.

• The constructor takes two parameters: initial value and upper limit
• When counter has reached the upper limit incrementing the value will set the
counter to zero.
• When counter is zero decrementing the counter sets counter to upper limit.


Implement function called UseCounter.

• void UseCounter(Counter& ctr, int num);
• Function takes two parameters: a reference to Counter and number that tells
how many times the counter should be incremented or decremented. A negative
value decrements counter and positive value increments counter.

Test your counters with different values and ways. Pay attention to the limits and make sure that they work properly.


SORRY THE CODE FORMAT IS NOT WORKING FOR ME


MY CODE:


#pragma once
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class Counter {
public:
virtual ~Counter() {};
virtual void inc() = 0;
virtual void dec() = 0;
virtual operator int() = 0;
};

void UseCounter(Counter& ctr, int num);

class LimitedCounter : public Counter {
public:
LimitedCounter(int in_val = 0, int up_lim = 0);

void inc() override = 0;
void dec() override = 0;
operator int() override = 0;

private:
int in_val;
int up_lim;
};

class OverflowCounter : public Counter {
public:
OverflowCounter(int in_val = 0, int up_lim = 0);

void inc() override = 0;
void dec() override = 0;
operator int() override = 0;

private:
int in_val;
int up_lim;
};

int main(int argc, char** argv) {
LimitedCounter lc(0, 5);
OverflowCounter oc(5, 9);

cout << oc << endl;
UseCounter(oc, 5);
cout << oc << endl; // should display zero
UseCounter(oc, -1);
cout << oc << endl; // should display 9
oc.dec();
cout << oc << endl; // should display 8

cout << lc << endl;
lc.inc();
cout << lc << endl;
lc.dec();
cout << lc << endl;
for (int i = 0; i < 10; ++i) lc.inc();
cout << lc << endl;
UseCounter(lc, -9);
cout << lc << endl;

return 0;
}
Last edited on
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
using namespace std;

class Counter {
public:
	Counter(int in, int li) : val(in), lim(li) {}
	virtual void inc() = 0;
	virtual void dec() = 0;

protected:
	int val {};
	int lim {};

	friend ostream& operator<<(ostream& os, const Counter& cnt);
};

ostream& operator<<(ostream& os, const Counter& cnt)
{
	return os << cnt.val << "  " << cnt.lim;
}

class LimitedCounter : public Counter {
public:
	LimitedCounter(int in, int up) : Counter(in, up) {}

	void inc() override { if (val < lim) ++val; }
	void dec() override { if (val > 0) --val; }
};

class OverflowCounter : public Counter {
public:
	OverflowCounter(int in, int up) : Counter(in, up) {}

	void inc() override { val = (val == lim) ? 0 : val + 1; }
	void dec() override { val = (val == 0) ? lim : val - 1; }
};

void UseCounter(Counter& ctr, int num)
{
	if (num > 0)
		for (int i = 0; i < num; ++i)
			ctr.inc();
	else
		if (num < 0)
			for (int i = num; i < 0; ++i)
				ctr.dec();
}

int main()
{
	LimitedCounter lc(0, 5);
	OverflowCounter oc(5, 9);

	cout << "oc " << oc << endl;

	UseCounter(oc, 5);
	cout << "oc +5 " << oc << endl; // should display zero

	UseCounter(oc, -1);
	cout << "oc -1 " << oc << endl; // should display 9

	oc.dec();
	cout << "oc dec " << oc << endl; // should display 8

	cout << "lc " << lc << endl;

	lc.inc();
	cout << "lc inc " << lc << endl;

	lc.dec();
	cout << "lc dec " << lc << endl;

	for (int i = 0; i < 10; ++i)
		lc.inc();

	cout << "lc 10 inc " << lc << endl;

	UseCounter(lc, -9);
	cout << "lc -9 " << lc << endl;
}



oc 5  9
oc +5 0  9
oc -1 9  9
oc dec 8  9
lc 0  5
lc inc 1  5
lc dec 0  5
lc 10 inc 5  5
lc -9 0  5


It worked perfect. Thanks!
Topic archived. No new replies allowed.