Method Chaining with this

#include <iostream>
using namespace std;

class Counter {
private:
    int value;
public:
    Counter() : value(0) {}

    // increment by 1 and return the object itself
    Counter& increment() {
        value++;
        return *this;
    }

    // decrement by 1 and return the object itself
    Counter& decrement() {
        value--;
        return *this;
    }

    int getValue() const {
        return value;
    }
};

int main() {
    Counter c;

    c.increment().increment().decrement();

    cout << c.getValue();
    return 0;
}
Upvote
Downvote
Loading...

Input

Expected Output

1