All submissions

Method Chaining with this

#include <iostream>
using namespace std;

class Counter {
private:
   int value;
public:
   Counter() : value(0) {}
   //  Increment by 1 and return current object
    Counter& increment() {
        ++value;
        return *this;
    }

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


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

int main() {
   Counter c;
   // example chain of calls
   c.increment().increment().decrement();
   cout << c.getValue();
   return 0;
}
Loading...

Input

Expected Output

1