H-SC COMS 262 - Lecture 10 - Operators

Unformatted text preview:

Operators as FunctionsOperator OverloadingOperators as Non-member FunctionsOperators as Member FunctionsFacilitatorsOperatorsRobb T.KoetherOperators asFunctionsOperatorOverloadingOperators asNon-memberFunctionsOperators asMemberFunctionsFacilitatorsOperatorsLecture 10Robb T. KoetherHampden-Sydney CollegeFri, Feb 6, 2009OperatorsRobb T.KoetherOperators asFunctionsOperatorOverloadingOperators asNon-memberFunctionsOperators asMemberFunctionsFacilitatorsOutline1Operators as Functions2Operator Overloading3Operators as Non-member Functions4Operators as Member Functions5FacilitatorsOperatorsRobb T.KoetherOperators asFunctionsOperatorOverloadingOperators asNon-memberFunctionsOperators asMemberFunctionsFacilitatorsOperators as FunctionsDefinition (Operator)An operator is a function that can be represented by asymbol, such as + or*.Different operators may have different numbers ofargumentsUnary operators have 1 argument.Binary operators have 2 arguments.Ternary operators have 3 arguments.OperatorsRobb T.KoetherOperators asFunctionsOperatorOverloadingOperators asNon-memberFunctionsOperators asMemberFunctionsFacilitatorsOperators as FunctionsUnary operators may be prefix or postfix.Most operators are binary.Most unary operators are prefix.OperatorsRobb T.KoetherOperators asFunctionsOperatorOverloadingOperators asNon-memberFunctionsOperators asMemberFunctionsFacilitatorsOperator OverloadingMost operators can be overloaded.Unary: +, -,*, &Arithmetic: +, -,*, /, %Equality: ==, !=Order: <, >, <=, >=Logical: &&, ||, !Bitwise: &, |, ~, ˆShift: <<, >>Assignment: =Compound assignment:+=, -=,*=, /=, %=, &=,|=, ˆ=, <<=, >>=Increment anddecrement: ++, -Allocation: new,deleteMiscellaneous: ,, ->*,->, (), []OperatorsRobb T.KoetherOperators asFunctionsOperatorOverloadingOperators asNon-memberFunctionsOperators asMemberFunctionsFacilitatorsOperator OverloadingA few operators cannot be overloaded.Member access: .Member access: .*Scope: ::Selection: ?:Size of: sizeofOperatorsRobb T.KoetherOperators asFunctionsOperatorOverloadingOperators asNon-memberFunctionsOperators asMemberFunctionsFacilitatorsOperators as Non-member FunctionsThe name of an operator function consists of thekeyword operator followed by the symbol for theoperator.The expressiona + bis interpreted asoperator+(a, b)OperatorsRobb T.KoetherOperators asFunctionsOperatorOverloadingOperators asNon-memberFunctionsOperators asMemberFunctionsFacilitatorsExampleExample (OperatorFunctions.cpp)Download and run OperatorFunctions.cpp.OperatorsRobb T.KoetherOperators asFunctionsOperatorOverloadingOperators asNon-memberFunctionsOperators asMemberFunctionsFacilitatorsBinary Operators as Member FunctionsOperator as Member FunctionType ClassName::operator+(Parameters);An operator may be defined as a member function of aclass.That may or may not be a good idea.A binary operator is invoked by the left operand of theexpression.Thus, the expression a + b is interpreted asa.operator+(b).OperatorsRobb T.KoetherOperators asFunctionsOperatorOverloadingOperators asNon-memberFunctionsOperators asMemberFunctionsFacilitatorsBinary Operators as Member Functions:ConsiderationsAdvantageThe operator has access to the private members of theleft operand.DisadvantagesIf a and b are objects of different classes, then a + band b + a will invoke different functions.The left operand may be a member of a class that wedo not have access to.OperatorsRobb T.KoetherOperators asFunctionsOperatorOverloadingOperators asNon-memberFunctionsOperators asMemberFunctionsFacilitatorsImplementing Binary Operators with FacilitatorsDefinition (Facilitator)A facilitator is a member function that is invoked by anon-member operator.The facilitator performs the work of the operator.The operator simplyInvokes the facilitator.Returns the appropriate object, typically the same onethat is returned by the facilitator.OperatorsRobb T.KoetherOperators asFunctionsOperatorOverloadingOperators asNon-memberFunctionsOperators asMemberFunctionsFacilitatorsBinary Operators with FacilitatorsA binary operator has two parameters.The corresponding facilitator has one parameter,namely, the right operand.If we write the facilitator as a member function, then wewrite the operator as a non-member function.The operator is invoked by the operands as an orderedpair.We may then use either operand to invoke thefacilitator.OperatorsRobb T.KoetherOperators asFunctionsOperatorOverloadingOperators asNon-memberFunctionsOperators asMemberFunctionsFacilitatorsThe PrototypesThe prototype of the facilitator isType1 Type2::FacilitatorName(Type3 obj3);The prototype of the operator isType1 operator+(Type2 obj2, Type3 obj3);orType1 operator+(Type3 obj3, Type2 obj2);OperatorsRobb T.KoetherOperators asFunctionsOperatorOverloadingOperators asNon-memberFunctionsOperators asMemberFunctionsFacilitatorsBinary Operators with Facilitators:ConsiderationsAdvantagesThe left operand need not be an object of the sameclass as the facilitator.The expressions a + b and b + a can be handled bythe same facilitator, even if a and b are objects ofdifferent types.DisadvantageRequires an additional function call.OperatorsRobb T.KoetherOperators asFunctionsOperatorOverloadingOperators asNon-memberFunctionsOperators asMemberFunctionsFacilitatorsUsing Operators with Mixed TypesExample (Operators with Mixed Types)Complx operator+(const Complx& c, int n){return c.add(Complx(n));}Complx operator+(int n, const Complx& c){return


View Full Document

H-SC COMS 262 - Lecture 10 - Operators

Download Lecture 10 - Operators
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 10 - Operators and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 10 - Operators 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?