I am learning C++ from a book and I find references and operator overloading really difficult. While studying operator overloading, I can't understand why this program won't work without a constructor that takes an integer as a parameter.
#include <iostream> using namespace std; class counter { public: counter(); counter(int value); ~counter(); int getValue() const { return itsValue; } int setValue(int value) { itsValue = value; } counter operator+(const counter&); private: int itsValue; }; counter::counter() : itsValue(0) {} counter::counter(int value) : itsValue(value) {} counter counter::operator+(const counter& rhs) { return itsValue + rhs.getValue(); } int main() { std::cout << "Hello World!\n"; } I can't understand why the program won't work in the absence of these lines:
counter(int value); AND
counter::counter(int value) : itsValue(value) {}