[Solved] Function don’t work in the class (c++)


Below is my attempt. There were some formatting issues but the trickiest problem was that getValue() is a const function.

class Int {
 private:
  int num;

 public:
  Int() { num = 0; }
  Int(int n) { num = n; }

  // getValue is a const function as it does not alter object 'n'.
  int getValue() const { return num; }
  int sum(const Int& n) { 
    int ret = num + n.getValue(); 
    return ret;
  }
};

int main() {
    int a, b;
    std::cout << "Enter 2 numbers:";
    std::cin >> a >> b;
    Int A(a), B(b), res;
    std::cout << A.getValue() << " + " << B.getValue() << " = " << A.sum(B) << "\n\n";
}

1

solved Function don’t work in the class (c++)