Friends and Unary Operators

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

The Class Construct Part 1

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

Scope and Parameter Passing

Recursive Linked Lists

List Iterator Implementation

Scope and Parameter Passing

LR Parsing - Conflicts

The Critical-Path Algorithm

Implementing Linked Lists

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

Minimal Spanning Trees

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

The Class Construct Part 2

Programming Languages

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

Stacks and their Applications

The Traveling Salesman Problem Brute Force Method

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

Magnification and Minification

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

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

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

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

Dynamic Allocation of Memory

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

Solving Recursive Sequences by Iteration

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Programming Languages

The x86 Architecture

XQuery FLOWR Expressions Lecture 35

Scheduling and Digraphs

The Decreasing-Time Algorithm

The Pairwise-Comparison Method

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

The Plurality-with-Elimination Method

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

Displaying Distributions - Quantitative Variables

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

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

Binary Tree Applications

Sampling Distribution Examples Sections 15.4, 15.5

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

Rotations and Translations

Density Curves Sections

The Graphics Pipeline

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

The x86 Instruction Set

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

Inheritance: The Fundamental Functions

Inheritance: The Fundamental Functions

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

Binary Tree Implementation

Recursive Descent Parsers

Introduction to Compiler Design

Recognition of Tokens

LR Parsing - The Items

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

Dynamic Allocation of Memory

Basic PHP Lecture 17

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

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

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

Building the Abstract Syntax Trees

Binary Tree Implementation

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

The CYK Parsing Algorithm

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

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

The Coefficient of Determination

The Graphics Pipeline

Fundamental Data Types

Function Definition Syntax Tree

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

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

The x87 Floating-Point Unit

Street-Routing Problems

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

The Normal Distribution

Chapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved

The Projection Matrix

Views. Lecture 15. Robb T. Koether. Fri, Feb 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, / 28

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

Lecture 29 Section Wed, Apr 1, 2009

Nondeterministic Programming in C++

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

Ambient and Diffuse Light

The Projection Matrix

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Operation Overloading.

Abstract Syntax Trees Synthetic and Inherited Attributes

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

Basic CSS Lecture 17

PHP Queries and HTML Forms Lecture 23

jquery Lecture 34 Robb T. Koether Wed, Apr 10, 2013 Hampden-Sydney College Robb T. Koether (Hampden-Sydney College) jquery Wed, Apr 10, / 29

AJAX. Lecture 26. Robb T. Koether. Fri, Mar 21, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) AJAX Fri, Mar 21, / 16

Transcription:

Friends and Unary Operators Lecture 11 Sections 11.3, 11.6 Robb T. Koether Hampden-Sydney College Fri, Feb 13, 2015 Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 1 / 23

1 Operators as Friends 2 Operators as Member Functions Operators that Must be Member Functions Unary Operators 3 The Increment and Decrement Operators 4 Assignment Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 2 / 23

Outline 1 Operators as Friends 2 Operators as Member Functions Operators that Must be Member Functions Unary Operators 3 The Increment and Decrement Operators 4 Assignment Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 3 / 23

Operators as Friends Definition (Friend) A friend of a class is a function or a class that is given access to the private members of that class through the keyword friend. The class must declare who its friends are. Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 4 / 23

Operators as Friends Friends class class-name { friend function(parameters); friend other-class-name; } To make a function (an operator) or another class a friend of this class, use the friend keyword in the definition of this class. Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 5 / 23

Writing Operators as Friends Declare the operator to be a friend of the class. Write the operator as a non-member function, except that... The operator may access the data members of the operands directly. Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 6 / 23

Operators as Friends: Considerations Advantages Only one function call is needed. The operator has direct access to the data members. Disadvantages Friendship violates the data-hiding principle. Now that the function has access to the private data members, it can do anything it wants. The integrity of the class is no longer under the control of the class. Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 7 / 23

Operators as Friends Example (Operators as Friends) Vectr(friends).h. OperatorFriends.cpp. Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 8 / 23

Choosing a Method The preferred method is to use facilitators. Exceptions Operators that must be member functions. Unary operators. Binary operators in which the left operand will always be an object of the class. In the exceptional cases, write the operator as a member function. Only in very rare cases will we use friends. Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 9 / 23

Outline 1 Operators as Friends 2 Operators as Member Functions Operators that Must be Member Functions Unary Operators 3 The Increment and Decrement Operators 4 Assignment Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 10 / 23

Outline 1 Operators as Friends 2 Operators as Member Functions Operators that Must be Member Functions Unary Operators 3 The Increment and Decrement Operators 4 Assignment Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 11 / 23

Unary Operators The following operators must be implemented as member functions. The assignment operator =. The subscript operator []. Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 12 / 23

The Subscript Operator Prototypes <type2> operator[](<type1>) const; <type2>& operator[](<type1>); // Returns r-value // Returns l-value <type1> can be any type, but it is usually int. The operator will return a value of <type2>. Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 13 / 23

The Subscript Operator Usage int temp = list[i]; // r-value list[i] = list[i + 1]; // l- and r-values list[i + 1] = temp; // l-value Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 14 / 23

Outline 1 Operators as Friends 2 Operators as Member Functions Operators that Must be Member Functions Unary Operators 3 The Increment and Decrement Operators 4 Assignment Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 15 / 23

Unary Operators Unary operators should be implemented as member functions. The operator is invoked by a single operand. The expression *a is interpreted as a.operator*() There is no issue of left operand vs. right operand. Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 16 / 23

Outline 1 Operators as Friends 2 Operators as Member Functions Operators that Must be Member Functions Unary Operators 3 The Increment and Decrement Operators 4 Assignment Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 17 / 23

The Pre-Increment Operator The Pre-Increment Operator <type>& <type>::operator++() { // Increment the object.. return *this; } The pre-increment operator should return the object by reference. The expression uses the returned value. What will ++(++a) do? Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 18 / 23

The Post-Increment Operator The post-increment operator should return the object by value. Include one unused and unnamed int parameter to distinguish post-increment from pre-increment. The designers of C++ apologize for this completely artificial mechanism. Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 19 / 23

The Post-Increment Operator The Post-Increment Operator <type> <type>::operator++(int) { <type> original = *this; // Increment the object }. return original; The expression uses the returned value. What will (a++)++ do? What about ++(a++) and (++a)++? Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 20 / 23

The Increment and Decrement Operators Example (The Increment and Decrement Operators) RationalIncrement.cpp. IncrementTest.cpp will test ++(++a), (++a)++, ++(a++), (a++)++, etc. Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 21 / 23

Outline 1 Operators as Friends 2 Operators as Member Functions Operators that Must be Member Functions Unary Operators 3 The Increment and Decrement Operators 4 Assignment Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 22 / 23

Assignment Assignment Read Sections 11.3, 11.6. Robb T. Koether (Hampden-Sydney College) Friends and Unary Operators Fri, Feb 13, 2015 23 / 23