Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Similar documents
Friends and Unary Operators

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

The Class Construct Part 1

Stacks and their Applications

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

The Class Construct Part 2

LR Parsing - Conflicts

The Critical-Path Algorithm

Scope and Parameter Passing

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Minimal Spanning Trees

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

The Traveling Salesman Problem Brute Force Method

Recursive Linked Lists

Scope and Parameter Passing

Operators in C. Staff Incharge: S.Sasirekha

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

List Iterator Implementation

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Solving Recursive Sequences by Iteration

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Implementing Linked Lists

Inheritance: The Fundamental Functions

Magnification and Minification

Inheritance: The Fundamental Functions

Programming Languages

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Scheduling and Digraphs

The Decreasing-Time Algorithm

The Pairwise-Comparison Method

The Plurality-with-Elimination Method

Binary Tree Implementation

Binary Tree Applications

The x86 Architecture

Sampling Distribution Examples Sections 15.4, 15.5

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Rotations and Translations

LR Parsing - The Items

The Graphics Pipeline

XQuery FLOWR Expressions Lecture 35

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Basic PHP Lecture 17

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

The x86 Instruction Set

Programming Languages

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Binary Tree Implementation

Displaying Distributions - Quantitative Variables

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

Recursive Descent Parsers

Dynamic Allocation of Memory

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Review of the C Programming Language for Principles of Operating Systems

Operators. Java operators are classified into three categories:

Density Curves Sections

More Advanced Class Concepts

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

Building the Abstract Syntax Trees

Operation Overloading.

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

The CYK Parsing Algorithm

Infix to Postfix Conversion

Program construction in C++ for Scientific Computing

A flow chart is a graphical or symbolic representation of a process.

Overloaded Operators, Functions, and Students

Information Science 1

The Coefficient of Determination

Recognition of Tokens

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Ambient and Diffuse Light

The Graphics Pipeline

Operators & Expressions

Review of the C Programming Language

Expression Evaluation and Control Flow. Outline

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

The x87 Floating-Point Unit

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Expressions and Precedence. Last updated 12/10/18

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Street-Routing Problems

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Transcription:

Operators Lecture 12 Section 14.5 Robb T. Koether Hampden-Sydney College Fri, Feb 9, 2018 Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 1 / 21

Outline 1 Operators as Functions 2 Operator Overloading 3 Operators as Non-member Functions 4 Operators as Member Functions 5 Facilitators 6 Assignment Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 2 / 21

Outline 1 Operators as Functions 2 Operator Overloading 3 Operators as Non-member Functions 4 Operators as Member Functions 5 Facilitators 6 Assignment Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 3 / 21

Operators as Functions Definition (Operator) An operator is a function that can be represented by a symbol, such as + or *. Different operators may have different numbers of arguments Unary operators have 1 argument. Binary operators have 2 arguments. Ternary operators have 3 arguments. Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 4 / 21

Operators as Functions Unary operators may be prefix or postfix. Binary operators are infix. Most unary operators are prefix. What is an example of a prefix unary operator? What is an example of a postfix unary operator? Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 5 / 21

Outline 1 Operators as Functions 2 Operator Overloading 3 Operators as Non-member Functions 4 Operators as Member Functions 5 Facilitators 6 Assignment Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 6 / 21

Operator Overloading Most operators can be overloaded. Unary: +, -, *, & Arithmetic: +, -, *, /, % Equality: ==,!= Order: <, >, <=, >= Logical: &&,,! Bitwise: &,, ~, ˆ Shift: <<, >> Assignment: = Compound assignment: +=, -=, *=, /=, %=, &=, =, ˆ=, <<=, >>= Increment and decrement: ++, -- Allocation: new, delete Miscellaneous:,, ->*, ->, (), [] Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 7 / 21

Operator Overloading A few operators cannot be overloaded. Member access:. Member access:.* Scope: :: Selection:?: Size of: sizeof Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 8 / 21

Outline 1 Operators as Functions 2 Operator Overloading 3 Operators as Non-member Functions 4 Operators as Member Functions 5 Facilitators 6 Assignment Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 9 / 21

Operators as Non-member Functions The name of an operator function consists of the keyword operator followed by the symbol for the operator. The expression a + b is interpreted as operator+(a, b) Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 10 / 21

As a non-member function, an operator does not have direct access to the private data members of the objects. However, it may use the inspector functions to get copies of the data members. Although this works, it can be very awkward. Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 11 / 21

Outline 1 Operators as Functions 2 Operator Overloading 3 Operators as Non-member Functions 4 Operators as Member Functions 5 Facilitators 6 Assignment Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 12 / 21

Binary Operators as Member Functions Operator as Member Function Type ClassName::operator+(Parameters); An operator may be defined as a member function of a class. That may or may not be a good idea. A binary operator is invoked by the left operand of the expression. Thus, the expression a + b is interpreted as a.operator+(b). Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 13 / 21

Binary Operators as Member Functions: Considerations Advantage The operator has access to the private members of the left operand. Disadvantages If a and b are objects of different classes, then a + b and b + a will invoke different functions. The left operand may be a member of a class that we do not have access to. Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 14 / 21

Outline 1 Operators as Functions 2 Operator Overloading 3 Operators as Non-member Functions 4 Operators as Member Functions 5 Facilitators 6 Assignment Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 15 / 21

Implementing Binary Operators with Facilitators Definition (Facilitator) A facilitator is a member function that is invoked by a non-member operator. The facilitator performs the work of the operator. The operator simply Invokes the facilitator. Returns the appropriate object, typically the same one that is returned by the facilitator. return a.facilitator(b); Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 16 / 21

Binary Operators with Facilitators A 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 we write the operator as a non-member function. The operator is invoked by the operands as an ordered pair. We may then use either operand to invoke the facilitator. Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 17 / 21

Binary Operators with Facilitators: Considerations Advantages The left operand need not be an object of the same class as the facilitator. The expressions a + b and b + a can be handled by the same facilitator, even if a and b are objects of different types. Disadvantage Requires an additional function call. Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 18 / 21

Using Operators with Mixed Types Example (Operators with Mixed Types) Point operator+(double s, const Point & p) { return p.scalarmultiply(s); } Point operator*(const Point& p, double s) { return p.scalarmultiply(s); } Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 19 / 21

Outline 1 Operators as Functions 2 Operator Overloading 3 Operators as Non-member Functions 4 Operators as Member Functions 5 Facilitators 6 Assignment Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 20 / 21

Assignment Assignment Read Section 14.5. Robb T. Koether (Hampden-Sydney College) Operators Fri, Feb 9, 2018 21 / 21