0

I want to overload some operators multiple times and i am not sure how to do it i have a class

for example

int main(){
    addClass c1;
    addClass c2;
    addClass c1 = (c2 + 10 * c2 + 2 * c3)*c3; 
}

my problem is that i dont know how to make this many opreator ovrloading, I mean what do i need to return in ordear for it to continue to the next opreator? I tried that

    class addClass 
    //declration
        friend addClass & operator*(const double, const addClass &);

    addClass & operator*(double x, const addClass & a1)
    {
        int i;
        addClass add(a1.rank);
    add.res=x*a1.res
        }
        return res;
    }

but for the next oprators i run into some problems so i wanted to know what to do

Thank you!

2
  • 1
    What problems did you run into? Give specific error messages that you got. Commented Apr 27, 2020 at 20:27
  • Are you sure this is your actual code? It is syntactically incorrect. Additionally, there are no members called rank and res. Also, you return res, which is not a variable declared in your code. It would help if you provide us with the actual code and the error message. Commented Apr 27, 2020 at 20:28

1 Answer 1

1

The order of evaluation is the same as usual even for overloaded operators, so with this example where I've added printing in the operators ...

#include <iostream>

class addClass {
public:
    addClass() : addClass(0) {}

    // converting constructor
    addClass(int x) : value(x) {}

    addClass& operator+=(const addClass& rhs) {
        value += rhs.value;
        return *this;
    }
    addClass& operator-=(const addClass& rhs) {
        value -= rhs.value;
        return *this;
    }    
    addClass& operator*=(const addClass& rhs) {
        value *= rhs.value;
        return *this;
    }
    addClass& operator/=(const addClass& rhs) {
        value /= rhs.value;
        return *this;
    }

    int getValue() const { return value; }
private:
    int value;
};

std::ostream& operator<<(std::ostream& os, const addClass& ac) {
    return os << ac.getValue();
}

addClass operator+(addClass lhs, const addClass& rhs) {
    std::cout << lhs << '+' << rhs << '=';
    lhs += rhs;
    std::cout << lhs << '\n';
    return lhs;
}
addClass operator-(addClass lhs, const addClass& rhs) {
    std::cout << lhs << '-' << rhs << '=';
    lhs -= rhs;
    std::cout << lhs << '\n';
    return lhs;
}
addClass operator*(addClass lhs, const addClass& rhs) {
    std::cout << lhs << '*' << rhs << '=';
    lhs *= rhs;
    std::cout << lhs << '\n';
    return lhs;
}
addClass operator/(addClass lhs, const addClass& rhs) {
    std::cout << lhs << '/' << rhs << '=';
    lhs /= rhs;
    std::cout << lhs << '\n';
    return lhs;
}

int main(){
    addClass c1;
    addClass c2 = 100;
    addClass c3 = 2;
    c1 = (c2 + 10 * c2 / 2 * c3)*c3;
}

... you would get this output:

10*100=1000
1000/2=500
500*2=1000
100+1000=1100
1100*2=2200
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.