All submissions

Method Chaining with this

#include <iostream>
using namespace std;

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

   Counter& increment() {
       value += 1;
       return *this;   // return reference to allow chaining
   }

   Counter& decrement() {
       value -= 1;
       return *this;   // return reference to allow chaining
   }

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

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

Input

Expected Output

1