Overload Increment operator, ++ - C++ Class

C++ examples for Class:Operator Overload

Introduction

To overload an operator in a class using a member function.

 
returnType operatorsymbol(parameter list) 
{ 
   // body of overloaded member function 
} 
 

Demo Code

                                                     
#include <iostream> 
                                                      
class Counter //from   w ww. jav a 2 s.  c  om
{ 
public: 
    Counter(); 
    ~Counter(){} 
    int getValue() const { return value; } 
    void setValue(int x) { value = x; } 
    void increment() { ++value; } 
    const Counter& operator++(); 
                                                       
private: 
    int value; 
}; 
                                                       
Counter::Counter(): 
value(0) 
{} 
                                                     
const Counter& Counter::operator++() 
{ 
    ++value; 
    return *this; 
} 
                                                       
int main() 
{ 
    Counter c; 
    std::cout << "The value of c is " << c.getValue() << std::endl; 
    c.increment(); 
    std::cout << "The value of c is " << c.getValue() << std::endl; 
    ++c; 
    std::cout << "The value of c is " << c.getValue() << std::endl; 
    Counter a = ++c; 
    std::cout << "The value of a: " << a.getValue(); 
    std::cout << " and c: " << c.getValue() << std::endl; 
    return 0; 
}

Result


Related Tutorials