Conversion Operators: a constructor that takes an int and produces a Counter object. - C++ Class

C++ examples for Class:Operator Overload

Description

Conversion Operators: a constructor that takes an int and produces a Counter object.

Demo Code

                                                         
#include <iostream> 
                                                            
class Counter /*www .  j a  v a2s.  c  om*/
{ 
public: 
    Counter(); 
    ~Counter() {} 
    Counter(int newValue); 
    int getValue() const { return value; } 
    void setValue(int newValue) { value = newValue; } 
private: 
    int value; 
}; 
                                                            
Counter::Counter(): 
value(0) 
{} 
                                                          
Counter::Counter(int newValue): 
value(newValue) 
{} 
                                                            
int main() 
{ 
    int beta = 5; 
    Counter alpha = beta; 
    std::cout << "alpha: " << alpha.getValue() << std::endl; 
    return 0; 
}

Result


Related Tutorials