C++ Reference

Arithmetic Operators



 
 
 
 
 

+ ++ -
--
* \ %
= -= +=
*= /= %=

All of the implementations below are based on an integer implementation, though the int operations are already defined.  Therefore these implementations are not to actually be used, but simply to provide insight into how the operator is defined.

+

  This operator has precedence lower than all other arithmetic operators, with the exception of -, and = (this includes all forms of =, such as +=, *=, etc...).

    By default, this operator will perform addition of two numbers, for which operator+ is defined, and by default returns the result of the two values.

    Sample Implementation:  (For this one would have to have a plus function declared which returned an int, and knew how to add two numbers).
       int operator+(int a, int b) {
          return plus(a, b);
       }
 

-

    This operator has precedence lower than all other arithmetic operators, with the exception of +, and = (this includes all forms of =, such as +=, *=, etc...).

    By default, this operator will perform subtraction of two numbers, for which the operator- is defined, and by default returns the result of the two values.

    Sample Implementation: (This uses the plus function from above)

    int operator-(int a, int b) {
        return plus(a, -b);
   }

    In the above, we use the unary version of this operator.  The unary form simply performs the operator of subtracting the number from 1.
 
 

++

    This operator has precedence greater than all other arithmetic operators, except --.

    By default this operator sets the argument to which it is applied (as this is both a prefix and postfix operator...) to one greater than its current value.

    This operator has two forms, one of which is called the postfix operator, and operates on the argument to its left, the other of which is called the prefix operator, and operates on the argument to its right.  These two operators have somewhat different behaviors.

    The postfix operator has the behavior that it returns the previous value of the variable to which it is applied, this basically results in an effect similar to returning the variable's new value minus 1.

    The prefix operator has the behavior that it returns the new value of the variable to which it is applied.  This basically results in an effect similar to returning the variable's previous value plus 1.
 

    Sample Implementation:
    (This implementation uses the special variable this.  For more information on this, see documentation on Classes)

    // Post-fix operator
    int operator++() {
        int temp = *this;
        *this =  plus(*this, 1);
        return temp;
    }

    // Prefix operator
    int operator++(int) {
        *this = plus(*this, 1);
        return *this;
    }
 
 
 

--
    This operator has precedence greater than all other arithmetic operators, except ++.

    By default this operator sets the argument to which it is applied ( as this is both a prefix and postfix operator...) to one less than its current value.

    This operator has two forms, one of which is called the postfix operator, and operators on the argument to its left, the other of which is called the prefix operator, and operators on the argument to its left.  These two operators have somewhat different behaviors.

    The postfix operator has the behavior that it returns the previous value of the variable to which it is applied, this basically results in an effect similar to returning the variable's new value plus 1.

    The prefix operator has the behavior that it returns the new value of the variable to which it is applied.  This basically results in an effect similar to returning the variable's previous value minus 1.

    Sample Implementation:
    (This implementation uses the special variable this.  For more information on this, see documentation on Classes)

    // Post-fix operator
    int operator--() {
        int temp = *this;
        *this =  plus(*this, -1);
        return temp;
    }

    // Prefix operator
    int operator--(int) {
        *this = plus(*this, -1);
        return *this;
    }