Help with Abstract classes please?


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
The virtual functions should be overridden in the derived classes.

For example:

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
struct counter
{
    virtual ~counter() = default ;

    // https://en.cppreference.com/w/cpp/language/rule_of_three#Rule_of_five
    counter() = default ;
    counter( const counter& ) = default ;
    counter& operator= ( const counter& ) = default ;
    counter( counter&& ) = default ;
    counter& operator= ( counter&& ) = default ;

    virtual int increment() = 0 ;
    virtual int decrement() = 0 ;
    virtual operator int() const = 0 ;
};

struct limited_counter : counter
{
    limited_counter( int initial_value, int ulimit ) : upper_limit(ulimit), current_value(initial_value)
    {
        // sanity checks, adjust value if required
        if( ulimit < 0 ) throw "bad upper limit\n" ;
        if( current_value < 0 ) current_value = 0 ;
        else if( current_value > upper_limit ) current_value = upper_limit ;
    }

    // Counter can’t be incremented over the upper limit.
    // If inc() is called when counter has reached the upper limit nothing happens
    virtual int increment() override { if( current_value < upper_limit ) ++current_value ; return current_value ; }

    // • Counter can’t be decremented below zero. If counter is zero then calling dec() will have no effect
    virtual int decrement() override { if( current_value > 0 ) --current_value ; return current_value ; }

    virtual operator int() const override { return current_value ; }

    private:
        int upper_limit ;
        int current_value ;
};
@JLBorges: I'm from Argentina and you? :) I just did the modifications you suggested. Can you read it one more time to see if there any other errors please? I appreciate
I'm from India.
(My forum name JLBorges is that of the great Argentine writer. Read only in translation)

If you post your modified code, I'll have a look at it.
Yes I post the modified code. Thanks for this.

Glad to know that you like Borges. If I can help providing any info you want, just let me know. Im happy to help
Topic archived. No new replies allowed.