1 / 33

Overloading Operators

Overloading Operators. 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>”

sanam
Download Presentation

Overloading Operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Overloading Operators

  2. 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.

  3. 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.

  4. 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.

  5. Operator Calls Example: var1 * var2 (a binary operator)

  6. Operator Calls • Example: var1 * var2 (a binary operator) • var1 is the lhs operand, var2 is the rhs operand. • If operator * () is a non-member function, then var1 and var2 are arguments passed to fill two parameters. • If operator * () is a member function, then var1 is the calling object and var2 is passed to a parameter

  7. Operator Calls • Example: ~var1 (a unary operator) • var1 is the only operand. • If operator ~ () is a non-member function, then var1 is the argument passed to fill a single parameter. • If operator ~ () is a member function, then var1 is the calling object.

  8. Operators • insertion and extraction: >> and << • cout << var; • cin >> var;

  9. Operators • insertion and extraction: >> and << • arithmetic: +, -, *, and / • var1 + var2 • var1 - var2 • var1 * var2 • var1 / var2

  10. Operators • insertion and extraction: >> and << • arithmetic: +, -, *, and / • assignments: = • var1 = var2; • var1 = var2 + var3;

  11. Operators • insertion and extraction: >> and << • arithmetic: +, -, *, and / • assignments: = • arithmetic assignments: +=, -=, *=, and /= • var1 += var2; • var1 -= var2; • var1 *= var2; • var1 /= var2;

  12. Operators • insertion and extraction: >> and << • arithmetic: +, -, *, and / • assignments: = • arithmetic assignments: +=, -=, *=, and /= • brackets and parenthesis: [] and () • object[index_pos] = var; • object(var);

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

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

  15. Additional Rules You cannot create new operators.

  16. Additional Rules You cannot create new operators. If you overload an operator, at least one of the parameters must be a user-defined type.

  17. 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.

  18. 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.

  19. 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 = [] () ->

  20. 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.

  21. 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.

  22. 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.

  23. 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.

  24. Recall //fraction.h ... class Fraction { ... • friend Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs); • ... }; //fraction.cpp ... Fractionmult_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; }

  25. Revised! //fraction.h ... class Fraction { ... • friend Fraction operator* (const Fraction & lhs, const Fraction & rhs); • ... }; //fraction.cpp ... • Fractionoperator* (const Fraction & lhs, const Fraction & rhs) { Fraction result(lhs); return (result*=rhs); }

  26. Revised! //fraction.h ... class Fraction { ... • friend Fraction operator* (const Fraction & lhs, const Fraction & rhs); • ... }; //fraction.cpp ... • Fractionoperator* (const Fraction & lhs, const Fraction & rhs) { Fraction result(lhs); return (result*=rhs); } needs to be defined

  27. 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); • }

  28. *this int int pointer 5 x

  29. *this int int pointer 5 x x refers to the pointer

  30. *this int int pointer 5 x *x refers to the int being pointed at

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

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

  33. End of Session

More Related