Help with Abstract classes. Thanks

Hi, most of the code is done below and here's my task. Thanks for any help

I need to derive two classes from an abstract class and write a function
that helps in testing the classes. The starting point is an abstract class called Counter that defines an interface for incrementing and decrementing a counter and a conversion operator that can be used to read or print the current counter value.

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.


MY CODE:


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
#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
Firstly, fix the declaration of the SetObserver() method - there's something rather obvious missing from that line, which suggests you haven't even tried to compile it yet.

The next step would be to implement the SetObserver() method. That should be straightforward; the text of the question tells you exactly what to do.
Did you change your post? Why?

Haven't you just made this a duplicate of http://www.cplusplus.com/forum/general/274672/ - a thread in which you were already getting help? Why?
Last edited on
Topic archived. No new replies allowed.