This class has exactly one explicit constructor (ctor). A constructor is a special procedure which returns nothing (not even void!) and runs when a new instance of an object is created. There's also the concept of a destructor, which is bound to similar rules, except that it runs exactly when an existing instance of an object is destroyed.
A constructor is always named exactly the same as the class. In this case,
Calculator::Calculator()
is
Calculator
's only explicit constructor; the other functions you have written are simply "member functions".
The constructor's code is fine, but many of the member functions aren't (they do nothing.)
Consider
1 2 3 4
|
void Calculator::add(float addend)
{
this -> add;
}
|
It's reasonable to assume that a calculator's add button adds its operand to whatever value is on the display.
this->add;
doesn't do that. Add what? Add how?
Add the argument to the calculator's display value, and set that to the display value. Which calculator?
this
calculator.
1 2 3 4
|
void Calculator::add(float addend)
{
this->dv += addend;
}
|
In
Calculator::squareroot()
, there's the same problem, but it also indicates that if there is an error, no buttons except "clear" respond, and the calculator will display "ERROR" until the error state is cleared.
You can do that by checking the member variable "error" in every place it matters with an if statement, only doing the calculation if the calculator's not error'd out.
Unfortunately that means that you have to copy the same error-checking code 9 times, which is terrible because it's slow and also extremely error-prone (what if you have to change it later, but miss a spot?). If you're not free to modify the class, however, you're stuck doing that.