Overloading Operators http cs mst edu Operators Operators

  • Slides: 33
Download presentation
Overloading Operators http: //cs. mst. edu

Overloading Operators http: //cs. mst. edu

Operators § Operators are functions, but with a different kind of name – a

Operators § Operators are functions, but with a different kind of name – a symbol. § Functions have parameters to which you pass arguments; operators have parameters to which you pass operands. Operands = Arguments. § The name of a operator is “operator <symbol>” § Operators are called differently from functions. http: //cs. mst. edu

Operators § Operators are functions, but with a different kind of name – a

Operators § Operators are functions, but with a different kind of name – a symbol. § Functions have parameters to which you pass arguments; operators have parameters to which you pass operands. Operands = Arguments. § The name of a operator is “operator <symbol>” § Operators are called differently from functions. § We say “operator overloading” because all operators are already defined – you cannot create new operators. http: //cs. mst. edu

Operators § Operators are functions, but with a different kind of name – a

Operators § Operators are functions, but with a different kind of name – a symbol. § Functions have parameters to which you pass arguments; operators have parameters to which you pass operands. Operands = Arguments. § The name of a operator is “operator <symbol>” § Operators are called differently from functions. § We say “operator overloading” because all operators are already defined – you cannot create new operators. § An operator can be defined either as a member function of a class or a non-member function. http: //cs. mst. edu

Operator Calls § Example: var 1 * var 2 http: //cs. mst. edu (a

Operator Calls § Example: var 1 * var 2 http: //cs. mst. edu (a binary operator)

Operator Calls § Example: var 1 * var 2 (a binary operator) § var

Operator Calls § Example: var 1 * var 2 (a binary operator) § var 1 is the lhs operand, var 2 is the rhs operand. § If operator * () is a non-member function, then var 1 and var 2 are arguments passed to fill two parameters. § If operator * () is a member function, then var 1 is the calling object and var 2 is passed to a parameter http: //cs. mst. edu

Operator Calls § Example: ~var 1 (a unary operator) § var 1 is the

Operator Calls § Example: ~var 1 (a unary operator) § var 1 is the only operand. § If operator ~ () is a non-member function, then var 1 is the argument passed to fill a single parameter. § If operator ~ () is a member function, then var 1 is the calling object. http: //cs. mst. edu

Operators § insertion and extraction: >> and << § cout << var; § cin

Operators § insertion and extraction: >> and << § cout << var; § cin >> var; http: //cs. mst. edu

Operators § insertion and extraction: >> and << § arithmetic: +, -, *, and

Operators § insertion and extraction: >> and << § arithmetic: +, -, *, and / § § var 1 + var 2 var 1 - var 2 var 1 * var 2 var 1 / var 2 http: //cs. mst. edu

Operators § insertion and extraction: >> and << § arithmetic: +, -, *, and

Operators § insertion and extraction: >> and << § arithmetic: +, -, *, and / § assignments: = § var 1 = var 2; § var 1 = var 2 + var 3; http: //cs. mst. edu

Operators § § insertion and extraction: >> and << arithmetic: +, -, *, and

Operators § § insertion and extraction: >> and << arithmetic: +, -, *, and / assignments: = arithmetic assignments: +=, -=, *=, and /= § § var 1 += var 2; var 1 -= var 2; var 1 *= var 2; var 1 /= var 2; http: //cs. mst. edu

Operators § § § insertion and extraction: >> and << arithmetic: +, -, *,

Operators § § § insertion and extraction: >> and << arithmetic: +, -, *, and / assignments: = arithmetic assignments: +=, -=, *=, and /= brackets and parenthesis: [] and () § object[index_pos] = var; § object(var); http: //cs. mst. edu

Operators § § § insertion and extraction: >> and << arithmetic: +, -, *,

Operators § § § insertion and extraction: >> and << arithmetic: +, -, *, and / assignments: = arithmetic assignments: +=, -=, *=, and /= brackets and parenthesis: [] and () and many others! http: //cs. mst. edu

Operators § § § § insertion and extraction: >> and << arithmetic: +, -,

Operators § § § § insertion and extraction: >> and << arithmetic: +, -, *, and / assignments: = arithmetic assignments: +=, -=, *=, and /= brackets and parenthesis: [] and () and many others! FORBIDDEN OVERLOADS: : : . . * sizeof ? : http: //cs. mst. edu

Additional Rules § You cannot create new operators. http: //cs. mst. edu

Additional Rules § You cannot create new operators. http: //cs. mst. edu

Additional Rules § You cannot create new operators. § If you overload an operator,

Additional Rules § You cannot create new operators. § If you overload an operator, at least one of the parameters must be a user-defined type. http: //cs. mst. edu

Additional Rules § You cannot create new operators. § If you overload an operator,

Additional Rules § You cannot create new operators. § If you overload an operator, at least one of the parameters must be a user-defined type. § You cannot change the arity of a operator. http: //cs. mst. edu

Additional Rules § You cannot create new operators. § If you overload an operator,

Additional Rules § You cannot create new operators. § If you overload an operator, at least one of the parameters must be a user-defined type. § You cannot change the arity of a operator. § You cannot change the order of precedence of operators by overloading them. http: //cs. mst. edu

Additional Rules § You cannot create new operators. § If you overload an operator,

Additional Rules § You cannot create new operators. § If you overload an operator, at least one of the parameters must be a user-defined type. § You cannot change the arity of a operator. § You cannot change the order of precedence of operators by overloading them. § Certain operators must be overloaded as class members. They are = [] () -> http: //cs. mst. edu

Additional Rules § You cannot create new operators. § If you overload an operator,

Additional Rules § You cannot create new operators. § If you overload an operator, at least one of the parameters must be a user-defined type. § You cannot change the arity of a operator. § You cannot change the order of precedence of operators by overloading them. § Certain operators must be overloaded as class members. They are = [] () -> § An overloaded operator cannot have default arguments. http: //cs. mst. edu

Rules of Thumb § Make your definitions of an overloaded operator make sense. As

Rules of Thumb § Make your definitions of an overloaded operator make sense. As an example, you would not want to define ‘+’ for complex numbers as subtraction. http: //cs. mst. edu

Rules of Thumb § Make your definitions of an overloaded operator make sense. As

Rules of Thumb § Make your definitions of an overloaded operator make sense. As an example, you would not want to define ‘+’ for complex numbers as subtraction. § Define an operator overload as a member function if it modifies the calling object; as a nonmember function if it doesn’t. http: //cs. mst. edu

Rules of Thumb § Make your definitions of an overloaded operator make sense. As

Rules of Thumb § Make your definitions of an overloaded operator make sense. As an example, you would not want to define ‘+’ for complex numbers as subtraction. § Define an operator overload as a member function if it modifies the calling object; as a nonmember function if it doesn’t. § Define symmetric operator pairs in terms of one another. As an example, define != using the == operator you already defined. http: //cs. mst. edu

Recall //fraction. h. . . class Fraction { . . . friend Fraction mult_fracs(const

Recall //fraction. h. . . class Fraction { . . . friend Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs); . . . }; //fraction. cpp. . . Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs) { Fraction temp; temp. m_Numerator = lhs. m_Numerator * rhs. m_Numerator; temp. m_Denominator = lhs. m_Denominator * rhs. m_Denominator; return temp; } http: //cs. mst. edu

Revised! //fraction. h. . . class Fraction { . . . friend Fraction operator*

Revised! //fraction. h. . . class Fraction { . . . friend Fraction operator* (const Fraction & lhs, const Fraction & rhs); . . . }; //fraction. cpp. . . Fraction operator* (const Fraction & lhs, const Fraction & rhs) { Fraction result(lhs); return (result*=rhs); } http: //cs. mst. edu

Revised! //fraction. h. . . class Fraction { . . . friend Fraction operator*

Revised! //fraction. h. . . class Fraction { . . . friend Fraction operator* (const Fraction & lhs, const Fraction & rhs); . . . }; //fraction. cpp. . . Fraction operator* (const Fraction & lhs, const Fraction & rhs) { Fraction result(lhs); return (result*=rhs); } needs to be defined http: //cs. mst. edu

Revised! //fraction. h. . . class Fraction { . . . friend Fraction operator*

Revised! //fraction. h. . . class Fraction { . . . friend Fraction operator* (const Fraction & lhs, const Fraction & rhs); Fraction& operator*= (const Fraction & rhs); . . . }; //fraction. cpp. . . Fraction& Fraction: : operator*= (const Fraction & rhs) { m_Numerator*=rhs. m_Numerator; m_Denominator*=rhs. m_Denominator; return (*this); } http: //cs. mst. edu

*this int pointer x 5 http: //cs. mst. edu

*this int pointer x 5 http: //cs. mst. edu

*this int pointer x 5 x refers to the pointer http: //cs. mst. edu

*this int pointer x 5 x refers to the pointer http: //cs. mst. edu

*this int pointer x 5 *x refers to the int being pointed at http:

*this int pointer x 5 *x refers to the int being pointed at http: //cs. mst. edu

Fraction pointer *this refers to the pointer that is keeping track of your Fraction

Fraction pointer *this refers to the pointer that is keeping track of your Fraction object’s place in memory – pointing to the calling object of this function Fraction& Fraction: : operator*= (const Fraction & rhs) { m_Numerator*=rhs. m_Numerator; m_Denominator*=rhs. m_Denominator; return (*this); } http: //cs. mst. edu

Fraction pointer *this refers to the fraction object currently executing your call to *=

Fraction pointer *this refers to the fraction object currently executing your call to *= the calling object Fraction& Fraction: : operator*= (const Fraction & rhs) { m_Numerator*=rhs. m_Numerator; m_Denominator*=rhs. m_Denominator; return (*this); } http: //cs. mst. edu

End of Session http: //cs. mst. edu

End of Session http: //cs. mst. edu