41. Method Chaining with this

 Define a class Counter that allows method chaining.

  • The class has a private variable value.
  • The constructor initializes value to 0.
  • Two methods:
    • increment() increases value by 1.
    • decrement() decreases value by 1. 
       

Both methods should return *this so that multiple calls can be chained like:
 c.increment().increment().decrement();

 

Example

Counter c;
c.increment().increment().decrement();
cout << c.getValue();

Output:

1
Loading...

Input

Expected Output

1