problem with overloading operators

So, for my c++ course, I am trying to make a "Calculator" class that is basically teaching us how to overload operators and organize a basic class.

my class only contains one private variable so far, called screen, which is holding the number which is having the arithmetic being applied to it.

my problem is that the arithmetic is not being applied correctly in the line
((((a+3.12)+5)-2)*3)/5;
my output is only 3.12, so it is only doing the a+3.12 in the innermost parenthesis
I suspect that this may be because I am not returning the object from the operator overload function correctly. Any help would be greatly appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;

class Calc{
      private:
              double screen;
      public:
             Calc();
             Calc(double value);
             Calc operator+(double val);
             Calc operator-(double val);
             Calc operator*(double val);
             Calc operator/(double val);
             void getscreen();
             };
int main(){
    Calc a;
    ((((a+3.12)+5)-2)*3)/5;
    a.getscreen();
    system("PAUSE");
    return 0;
}
Calc::Calc(){
             screen = 0;
             }
Calc::Calc(double value){
                  screen = value;
                  }
Calc Calc::operator +(double val){
           screen+=val;
           return *this;
          }
Calc Calc::operator -(double val){
           screen-=val;
           return *this;
          }
Calc Calc::operator *(double val){
           screen*=val;
           return *this;
          }
Calc Calc::operator /(double val){
           screen/=val;
           return *this;
          }
void Calc::getscreen(){
                cout<<screen<<endl;
                }

                  
I think your suspicion is correct.

Actually, the line

((((a+3.12)+5)-2)*3)/5;

should not change a at all, given the expected behavior of +, -, etc. But this should

a = ((((a+3.12)+5)-2)*3)/5;

operator+=, operator-=, etc act on the current object, but not operator+, operator-, etc

As it stands, you operator+ looks more like an operator+=, with screen being the value of the instance.
Last edited on
Thanks for the reply!

Unfortunately, I cannot change that line, as it was given with the assignment and I need to make my class work with how it is.

I am not sure how I can make it work with
((((a+3.12)+5)-2)*3)/5;

Here is how I understand my code, which must be incorrect since I dont understand why its not working as I expect:

a+3.12 ----->when the + operator is seen with a double on the right side, it goes into my +operator function, which adds the double value to screen, then the object(with updated screen) is returned to main

now, that line is more like (((a+5)-2)*3)/5; with a being the object returned by the first operation
and the rest of the arithmetic would behave similarly...is this not how the object is being returned to main?
a+3.12 ----->when the + operator is seen with a double on the right side,
> it goes into my +operator function, which adds the double value to screen,

Yes.

> then the object(with updated screen) is returned to main

No. Then a copy of the object (with updated screen) is returned to main. main() then performs the next operation on this copy instead of on the original 'a' object.

Return the object instead of a copy, and you would get the behaviour that you expect.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Calc
{
    // ...
    // Calc operator+(double val);
    Calc& operator+(double val);
    // ...
};

// Calc Calc::operator +(double val){
Calc& Calc::operator +(double val){
           screen+=val;
           return *this;
          }

And likewise for the -, * and / operators.



Alternatively, in main() you could write:
1
2
// ((((a+3.12)+5)-2)*3)/5;
a = ((((a+3.12)+5)-2)*3)/5;

The final copy would then be copied back to the original 'a' object.
Last edited on
Your operators return a copy of the object, so it's more like a+3.12 gets evaluated and then you have (((copy_of_a+5)-2)*3)/5, and because you have no way to access those temporary objects, the results of the calculation are lost.

Unfortunately, I cannot change that line, as it was given with the assignment and I need to make my class work with how it is.


That is unfortunate. I would verify with your instructor that you understand the assignment correctly, because that is certainly not the way one would normally implement those operators.
ok, that makes alot of sense. Thank you so much for your time and help!
Topic archived. No new replies allowed.