Computer Science II CSci 1200 Lecture 18 Operators and Friends

Similar documents
CS11 Intro C++ Spring 2018 Lecture 5

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

ADTs & Classes. An introduction

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

Object-Oriented Principles and Practice / C++

Operator overloading

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

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

Overloading & Polymorphism

CSCI-1200 Data Structures Spring 2018 Lecture 15 Associative Containers (Maps), Part 2

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Ch. 12: Operator Overloading

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

CSCI-1200 Data Structures Fall 2016 Lecture 17 Associative Containers (Maps), Part 2

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2

CSCI 102 Fall 2010 Exam #1

Lesson 12 - Operator Overloading Customising Operators

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Operator overloading: extra examples

And Even More and More C++ Fundamentals of Computer Science

Intermediate Programming, Spring 2017*

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

PIC 10A Objects/Classes

Roxana Dumitrescu. C++ in Financial Mathematics

More Advanced Class Concepts

C++ for Java Programmers

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

Shahram Rahatlou. Computing Methods in Physics. Overloading Operators friend functions static data and methods

OBJECT ORIENTED PROGRAMMING USING C++

Arizona s First University. More ways to show off--controlling your Creation: IP and OO ECE 373

Absolute C++ Walter Savitch

Fast Introduction to Object Oriented Programming and C++

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

B16 Object Oriented Programming

Overloaded Operators, Functions, and Students

Determine a word is a palindrome? bool ispalindrome(string word, int front, int back) { Search if a number is in an array

Object Oriented Software Design II

6. Pointers, Structs, and Arrays. 1. Juli 2011

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

B16 Object Oriented Programming

Overloading Operators in C++

11.2. Overloading Operators

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Short Notes of CS201

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

CSE 303: Concepts and Tools for Software Development

CS 247: Software Engineering Principles. ADT Design

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CS201 - Introduction to Programming Glossary By

CS3157: Advanced Programming. Outline

Chapter 11: More About Classes and Object-Oriented Programming

7.1 Optional Parameters

Plan of the day. Today design of two types of container classes templates friend nested classes. BABAR C++ Course 103 Paul F. Kunz

Programming Abstractions

pointers + memory double x; string a; int x; main overhead int y; main overhead

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

A brief introduction to C++

Chapter 13: Copy Control. Overview. Overview. Overview

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

Lecture 14: more class, C++ streams

AIMS Embedded Systems Programming MT 2017

Review of Important Topics in CS1600. Functions Arrays C-strings

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Object-Oriented Design (OOD) and C++

Problem Solving with C++

More class design with C++ Starting Savitch Chap. 11

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

CPSC 427a: Object-Oriented Programming

COMP 2355 Introduction to Systems Programming

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

Lesson 13 - Vectors Dynamic Data Storage

QUIZ. What is wrong with this code that uses default arguments?

CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I

EL2310 Scientific Programming

Week 8: Operator overloading

G52CPP C++ Programming Lecture 17


(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

Friends and Overloaded Operators

Friend Functions and Friend Classes

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Friends and Overloaded Operators

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Lab 2: ADT Design & Implementation

CS201 Latest Solved MCQs

CSCI-1200 Data Structures Spring 2018 Lecture 16 Trees, Part I

8. Object-oriented Programming. June 15, 2010

10. Object-oriented Programming. 7. Juli 2011

Transcription:

Review from Lecture 17 Arrays and pointers Computer Science II CSci 1200 Lecture 18 Operators and Friends Different types of memory Dynamic allocation of arrays Today s Lecture Operators and Friends Chapter 12 Review of operators as non-member functions. Operators as member functions Example: the Complex class. Friend functions and classes Stream operators Review of Non-Member Function Operators We have already written our own operators, especially operator<, as non-member functions. We used this operator to sort objects and to create our own keys for maps. Recall the example of the Name. class Name { public: Name( string const& first, string const& last ) : first_( first ), last_( last ) {} const string& first() const { return first_; } const string& last() const { return last_; }

private: string first_; string last_; }; operator< applied to the Name class behaves as Name n1( "Boston", "RedSox" ); Name n2( "StLouis", "Cardinals" ); if ( n1 < n2 ) cout << "It s the American League champion.\n"; When this operator is not a member function of the Name class, the expression n1 < n2 is translated by the compiler into the function call operator< ( n1, n2 ); This is why we wrote the operator as bool operator< ( Name const& left, Name const& right ) { return left.last() < right.last() ( left.last() == right.last() && left.first() < right.first() ); } The operator does not need to be a member function because it can access all of the information it needs through the public interface to the Name class. Operators As Member Functions Operators can also be written as member functions. The syntax is a bit different, so we need to be careful. Let s rewrite operator< as a member function of the Name class: class Name { public: Name( string const& first, string const& last ) : first_( first ), last_( last ) {} 2

const string& first() const { return first_; } const string& last() const { return last_; } bool operator< ( Name const& right ) const; private: string first_; string last_; }; Then in the Name.cpp file (or whatever we call it), the operator (function) would be defined as: bool Name :: operator< ( Name const& right ) const { return last_ < right.last_ ( last_ == right.last_ && first_ < right.first_ ); } The expression n1 < n2 is translated by the compiler into n1.operator< ( n2 ); This shows that the version of operator< called is the member function of n1, since n1 appears on the left-hand side of the operator. Observe that the function called now has only one argument! There are several important properties of the implementation of operator< as a member function: It is within the scope of class Name, so private member variables can be accessed directly. The member variables of n1, whose member function is actually called, are referenced by just using their names. The member variables of n2 are accessed using the right. qualifier, since right is the parameter that n2 is passed to. The member function is const, which means that n1 will not (can not) be changed by the function. 3

Complex Numbers A Brief Review We use implemenation of a complex number class to to explore operators in more depth, showing how to make our own classes act and feel like built-in types. Complex numbers take the form z = a + bi, where i = 1 and a and b are real. a is called the real part, b is called the imaginary part. If w = c + di, then w + z = (a + c) + (b + d)i, w z = (a c) + (b d)i, and w z = (ac bd) + (ad + bc)i The magnitude of a complex number is a 2 + b 2. The Complex Class A good start on a Complex class is provided in the attachment to the notes. Three files are provided: Complex.h, Complex.cpp, and complexmain.cpp. The class has two private member variables to represent the real and imaginary parts. Several constructors have been defined. There are functions to set the values of the private member variables and to access these values. Several different operators have been provided already Some of these are member functions, some are non-member functions, and some are special non-member functions called friend functions. We will discuss the following in turn: 4

Arithmetic operators Assignment operators Friend functions and operators as friend functions Stream operators Binary Arithmetic Operators operator+ is defined as a member function, while operator- is defined as a non-member function Just like in the Name class, this difference determines how they access the contents of the Complex objects the work on: operator+ accesses contents directly operator- accesses contents indirectly, through public member functions. This difference also determines how the compiler calls the functions: z + w becomes z.operator+ ( w ) z - w becomes operator- ( z, w ) Both return Complex objects, so both must call Complex constructors to create these objects. Exercises Calling constructors for Complex objects inside functions, especially member functions that work on Complex objects, seems somewhat counter-intuitive at first, but it is common practice! 1. Write operator* for Complex numbers as a member function of the Complex class. Show the additions to Complex.h and Complex.cpp. 2. Write operator* for Complex numbers as an ordinary function instead of as a member function of the Complex class. Show the additions to Complex.h and Complex.cpp. 5

Driver Main Program This is the file complexmain.cpp Note how it exercises every member function, sometimes multiple times. Such driver programs are good for testing the classes you write. Assignment Operators The assignment operator, used in the main program as z1 = z2; becomes a function call z1.operator=( z2 ); Cascaded assignments like z1 = z2 = z3; are really z1 = (z2 = z3); which becomes z1.operator= ( z2.operator= ( z3) ); Studying these helps to explain how to write the assignment operator, usually as a member function. The argument the right side of the operator is passed by constant reference. Its values are used to change the contents of the left side of the operator, which is the object whose member function is called. A reference to this object is returned, allowing a subsequent call to operator= - z1 s operator= in the example above. 6

The identifier this is reserved as a pointer inside class scope to the object whose member function is called. Therefore, *this is a a reference to this object. The fact that operator= returns a reference allows us to write code of the form (z1 = z2).real(); Exercise Write an operator+= as a member function of the Complex class. To do so, you must combine what you learned about operator= and operator+. In particular, the new operator must return a reference, *this. Returning Objects vs. Returning References to Objects When you create a new object inside an operator or member function, you must simply return it. This is why the return types of operator+ and operator- are both Complex. Technically, this copies the contents of the locally-created new objects into yet another object the object returned. This places the returned object outside of the scope of the function. Good compilers recognize this and avoid the work of creating an extra object. When you change an existing object inside an operator and need to return that object, you must return a reference to that object. This is why the return types of operator= and operator+= are both Complex&. This avoids creation of a new object. A common error made by beginners (and some non-beginners!) attempting to return a reference to a locally created object! is 7

Friend classes We re now going to shift gears slightly and discuss friend classes and functions. This will lead to the third method of writing an operator. One class (call it Foo) can designate another class (call it Bar) to be a friend. This allows member functions in class Bar to access the private member functions and variables of a Foo object as though they were public. This must be done in the public area of the declaration of Foo, i.e. class Foo { public: friend class Bar;... }; Note that Foo is giving friendship (access to its private contents) rather than Bar claiming it. Think about what might happen if Bar really could claim it! Friendship is often used for closely related (interdependent) classes. Friend Functions One class (call it Foo) can designate a specific function (call it f1) to be a friend. Within the scope of the definition of f1, private member functions and variables of Foo objects can be accessed directly, as though they were public. The most common example of this is operators, and especially stream operators. We will proceed to an example of this shortly. Note that stream operators can not be members; they must be friends. Reasons for this will be explained later. Friend Operators Let s re-write operator- as a friend function. 8

The declaration is moved from outside the class declaration to inside it, and the keyword friend is added to the front friend Complex operator- ( Complex const& lhs, Complex const& rhs ); The declaration is moved because a class must give friendship inside its declaration. Only one declaration is needed, so the operator declaration inside the class declaration suffices The operator definition inside Complex.cpp becomes Complex operator- ( Complex const& lhs, Complex const& rhs ) { return Complex( lhs.real_ + rhs.real_, lhs.imag_ + rhs.imag_ ); } Notice that because it is a friend, it can access private member variables directly, just as though it were within class scope. Stream Operators The operators >> and << are defined for the Complex class. These are binary operators. Recall that the consecutive calls to the << operator, such as cout << "z3 = " << z3 << endl; are really ((cout << "z3 = ") << z3) << endl; Each application of the operator returns an ostream object so that the next application can occur. A notation like cout << z3 9

is really operator<< ( cout, z3 ) To make this a member function, it would have to be a member function of the ostream class because this is the first argument. Hence, we can t make it a member function of the complex class. This is why the stream operators are never member functions. They could be either friend functions or ordinary non-member functions. Friend functions are more common. Ordinary non-member functions are fine as well, but only if the operators can do their work through the public class interface!! We ve written one stream operator as a friend and one as an ordinary non-member function for the Complex class. Summary of Operator Overloading Many operators can be overloaded, including operators we haven t discussed, such as operator++ operator-- operator[] operator() Yes, we can even overload the function call operator! In fact, we can overload 42 different operators. There are only 5 operators can not be overloaded! The most important syntactic rule is that overloading can never change the number of arguments or the form of an operator. The only exception to this is the function call operator, which already has a variable number of arguments. There are three different ways to overload an operator: Member function 10

Non-member function Friend function When there is a choice as to whether an operator should be a member, a non-member, or a friend, skilled programmers disagree about which is better. My preference is non-member first, then member, then friend. The most important rule for clean class design involving operators is to NEVER change the intuitive meaning of an operator. The whole point of operators is lost if you do. One (bad) example would be defining the increment operator on a Complex number. 11