CSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms

Similar documents
Chapter 18 - C++ Operator Overloading

Operator Overloading

Friends and Overloaded Operators

Operator Overloading in C++ Systems Programming

Friends and Overloaded Operators

Introduction. W8.2 Operator Overloading

W8.2 Operator Overloading

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

IS 0020 Program Design and Software Tools

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

Operator Overloading

Operation Overloading.

Ch 8. Operator Overloading, Friends, and References

Arrays. Week 4. Assylbek Jumagaliyev

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

Overloaded Operators, Functions, and Students

Program construction in C++ for Scientific Computing

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

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

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

CS 247: Software Engineering Principles. ADT Design

PIC10B/1 Winter 2014 Exam I Study Guide

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

Operator Overloading

Introduction to C++ Systems Programming

Review. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?

Informatik I (D-ITET) Übungsstunde 12,

IS 0020 Program Design and Software Tools

Operator overloading: extra examples

COMP322 - Introduction to C++

More Advanced Class Concepts

Module Operator Overloading and Type Conversion. Table of Contents

Overloading Operators in C++

Object oriented programming

Chapter 15 - C++ As A "Better C"

UNIT- 3 Introduction to C++

CS242 COMPUTER PROGRAMMING

W3101: Programming Languages C++ Ramana Isukapalli

Chapter 3. Numeric Types, Expressions, and Output

7.1 Optional Parameters

A <Basic> C++ Course

A <Basic> C++ Course

Abstraction and Encapsulation. Benefits of Abstraction & Encapsulation. Concrete Types. Using Typedefs to streamline classes.

Arizona s First University. ECE 373. Operation Overloading. The Revolution Operation Will Be Televised (and, these slides will be online later today)

11.2. Overloading Operators

14.1. Chapter 14: static member variable. Instance and Static Members 8/23/2014. Instance and Static Members

Pointers and Strings Prentice Hall, Inc. All rights reserved.

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

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Chapter 7. Additional Control Structures

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

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

CS11 Intro C++ Spring 2018 Lecture 5

PIC 10A Objects/Classes

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

ADTs & Classes. An introduction

CHAPTER 3 Expressions, Functions, Output

Object oriented programming

Programming C++ Lecture 6. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Overloading & Polymorphism

EL2310 Scientific Programming

2 ADT Programming User-defined abstract data types

Object-Oriented Principles and Practice / C++

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

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

Functions and Recursion

Chapter7 Expression and Assignment Statement. Introduction

IS0020 Program Design and Software Tools Midterm, Fall, 2004

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

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

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.

Evolution of Programming Languages

Where do we go from here?

Introduction to Programming

Instantiation of Template class

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

Programming in C++: Assignment Week 4

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

CS304 Object Oriented Programming Final Term

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

OBJECT ORIENTED PROGRAMMING USING C++

Ch. 12: Operator Overloading

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Classes and Data Abstraction. Topic 5

Roxana Dumitrescu. C++ in Financial Mathematics

Classes. Christian Schumacher, Info1 D-MAVT 2013

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Lecture 23: Pointer Arithmetic

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Object-Oriented Design (OOD) and C++

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

Chapter 9. Operator Overloading. Dr Ahmed Rafat

CS3157: Advanced Programming. Outline

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Pointers and Strings. Adhi Harmoko S, M.Komp

Transcription:

CSC 330 Object Oriented Programming Operator Overloading Friend Functions & Forms 1

Restrictions on Operator Overloading Most of C++ s operators can be overloaded. Operators that can be overloaded + - * / % ^ & ~! = < > += -= *= /= %= ^= &= = << >> >>= <<= ==!= <= >= && ++ -- ->*, -> [] () new delete new[] delete[] No new operators can be created Use only existing operators 2

Arithmetic Operators 3

Comparison Operators 4

Logical Operators 5

Bitwise Operator 6

Other Operators 7

Examples of Bitwise Operators 8

Restrictions on Operator Overloading cont d Arity (number of operands) cannot be changed Unary operators remain unary, and binary operators remain binary Operators &, *, + and - each have unary and binary versions Unary and binary versions can be overloaded separately 9

Overloading Operators The Rules Associativity refers to the order in which actions within an expression are carried out You cannot change associativity when you overload operators You also cannot change the normal precedence of any operator 10

Overloading Operators The Rules 11

A Complex Class #include <iostream> class Complex { double real; double image; public: Complex(double re=0, double im=0) { this->real = re; this->image = im; Complex add(const Complex& c) const { Complex result; result.real = this->real + c.real; result.image = this->image + c.image; return result;.... ; 12

Using the Complex Class int main() { Complex z(1,2), w(3,4); Complex a = z.add(w); a.display(); // want a = z+w (4,6) 13

Operator Functions operator keyword together with an operator operator+ operator- etc. Can overload all operators except: ::. *?: 14

Complex with operator+ class Complex { //... public: //... Complex operator+(const Complex& c) const { Complex result; result.real = this->real + c.real; result.image = this->image + c.image; return result; //... ; int main() { Complex z(1,2), w(3,4); Complex a = z + w; a.display(); // z.operator+(w) 15

Global Complex Operators class Complex { double real, image; public: ; Complex(double x = 0, double y = 0) { this->real = x; this->imag = y; double getreal() const {return real; double getimage() const {return image; //... Complex operator+(const Complex& c1, const Complex& c2) { double re = c1.getreal() + c2.getreal(); double im = c1.getimage() + c2.getimage(); Complex result(re, im); return result; 16

friend Functions class Complex { public: Complex( ); // default Complex( double ) ; // real given Complex( double, double ) ; // both given void write( ) const; // friend functions friend Complex operator+( const Complex&, const Complex& ) ; friend Complex operator-( ( const Complex&, const Complex& ) ; friend Complex operator*( const Complex&, const Complex& ) ; friend Complex operator/( const Complex&, const Complex& ) ; private: double real; double image; ; 17

Implementation File // Complex + as top-level friend Complex operator+( const Complex& t, const Complex& u ) { return Complex( t.real + u.real, t.image + u.image ) ; // Complex - as top-level friend Complex operator-( ( const Complex& t, const Complex& u ) { return Complex( t.real - u.real, t.image - u.image ) ; // Complex * as top-level friend Complex operator*( const Complex& t, const Complex& u ) { return Complex( t.real * u.real - t.imag * u.image, t.imag * u.real + t.real * u.image ) ; // Complex / as top-level friend Complex operator/( const Complex& t, const Complex& u ) { double abs_sq = u.real * u.real + u.image * u.image; return Complex( (t.real( * u.real + t.image * u.image)/abs_sq, (t.image * u.real - t-real * u.image)/abs_sq); ); 18

Discussions Friendship (i.e., using the friend keyword) is a complex and dangerous topic for various reasons: Friendship, when applied to program design, is an escape mechanism allowing us to circumvent the principles of encapsulation and data hiding. The use of friends should therefore be minimized to situations where they can be used naturally. If friends are used, realize that friend functions or classes become implementation dependent on the classes declaring them as friends. Once the internal organization of the data of a class declaring friends changes, all its friends s must be recompiled (and possibly modified) as well. Therefore, as a rule of thumb: don't use friend functions or classes. 19

Global Operator Overloading Similar to friend Function Overloading, Except the Keyword friend is Omitted and Global Functions CANNOT ACCESS private Members class Complex{ //All Public Members! public: int real, image; Complex(int ip1 = 0, int ip2 = 0) :real(ip1), image(ip2){; ; void operator!(complex a) { int temp = a.real; a.real = a.image; a.image = temp; cout << "Real: " << a.real << endl; cout << "Imaginary: " << a.image << endl; main() { Complex one(1,2);!one; 20

Overloading of Operators Having a Variable Arity Operators Such as - Can be Unary or Binary Overloading of Such Operators Involves Creating a Unary Function (One Operand) and a Binary Function (Two Operands) Only if Both Forms are Used, They Need to be Implemented class Number{ int n; public: Number(int x = 0):n(x){ Number operator-(){n = -n; return *this; Number operator-(number ip) {n = n ip.n; ; return *this; ; main(){ Number one(1), two(2), three; one = -one; //unary operator three = one - two; //three.n = -3 21

Operators with Prefix and Postfix Forms Separate Functions for Each -- Prefix and Postfix -- Forms are Needed Prefix Form is Treated as an Unary Operator Postfix Form is Treated as a Binary Operator 22

Prefix Overloaded Function -- Example class Number{ int n; public: Number(int x):n(x){; ){; //Constructor //prefix operator -- unary Number operator++(); ; Number Number::operator++(){ n++; return *this; main(){ Number one(10); //one.n = 10 ++one; //one.n = 11 23

Postfix Overloaded Function -- Example Postfix Operator is Implemented as a Binary Operator with an int Argument with a Default Value of 0. When specifying an overloaded operator for the postfix form of the increment or decrement operator, the additional argument must be of type int; specifying any other type generates an error. class Number{ int n; public: Number(int x):n(x){; ){; //Constructor //postfix operator -- binary -- int argument Number operator++(int); ; Number Number::operator++(int y) {if (y!= 0) n += y; else n++; return *this; 24

Postfix overloaded function Example (Cont) main() { Number one(10); // one.n = 10 one++; // one.n = 11 one.operator++(2); // one.n = 13 There is no syntax for using the increment or decrement operators to pass these values other than explicit invocation, as a shown in the preceding code. A more straightforward way to implement this functionality is to overload the addition/assignment operator (+=). 25

Special Overloading Forms A Few Operators Require Special Treatments During Overloading Conversion Operator const Array Operator Function Call -- Parenthesis Operator Stream Insertion -- << Operator Stream Extraction -- >> Operator Pointer to Member -- -> Operator Assignment Operator new Operator delete Operator 26

Overloading Stream-Insertion and Stream-Extraction Operators Overloaded << and >> operators Must have left operand of types ostream &, istream & respectively It must be a non-member function (left operand not an object of the class) It must be a friend function if it accesses private data members 27

1 // Fig. 18.3: fig18_03.cpp 2 // Overloading the stream-insertion and 3 // stream-extraction operators. 4 #include <iostream> 5 6 using std::cout; 7 using std::cin; 8 using std::endl; 9 using std::ostream; 10 using std::istream; 11 12 #include <iomanip> 13 14 using std::setw; 15 16 class PhoneNumber { 17 friend ostream &operator<<( ostream&, const PhoneNumber & ); 18 friend istream &operator>>( istream&, PhoneNumber & ); 19 20 private: 21 char areacode[ 4 ]; // 3-digit area code and null 22 char exchange[ 4 ]; // 3-digit exchange and null 23 char line[ 5 ]; // 4-digit line and null 24 ; 25 26 // Overloaded stream-insertion operator (cannot be 27 // a member function if we would like to invoke it with 28 // cout << somephonenumber;). 29 ostream &operator<<( ostream &output, const PhoneNumber &num ) 30 { 28

31 output << "(" << num.areacode << ") " 32 << num.exchange << "-" << num.line; 33 return output; // enables cout << a << b << c; 34 35 36 istream &operator>>( istream &input, PhoneNumber &num ) 37 { 38 input.ignore(); // skip ( 39 input >> setw( 4 ) >> num.areacode; // input area code 40 input.ignore( 2 ); // skip ) and space 41 input >> setw( 4 ) >> num.exchange; // input exchange 42 input.ignore(); // skip dash (-) 43 input >> setw( 5 ) >> num.line; // input line 44 return input; // enables cin >> a >> b >> c; 45 46 47 int main() 48 { 49 PhoneNumber phone; // create object phone 50 51 cout << "Enter phone Number in the form (123) 456-7890:\n"; 52 53 // cin >> phone invokes operator>> function by 54 // issuing the call operator>>( cin, phone ). 55 cin >> phone; 56 57 // cout << phone invokes operator<< function by 58 // issuing the call operator<<( cout, phone ). 59 cout << "The phone Number entered was: " << phone << endl; 60 return 0; 61 29

Enter phone Number in the form (123) 456-7890: (800) 555-1212 The phone Number entered was: (800) 555-1212 30

Example: Student Class class Student { public: Student(); Student( const string & id, const string & lname ); bool operator < (const Student & S2 ); Student & operator =(const = Student & S2 ); bool operator ==( ( const Student & S2 ); friend ostream & operator <<(ostream & out, const Student & S); friend istream & operator >>(istream & inp,, Student & S); private: string m_id; string m_lastname; ; 31

Overloading < bool operator <( const Student & S2 ) { return m_id < S2.m_ID; 32

Overloading = Student & operator =( const Student & S2 ) { // if assigning object to itself... if( *this == S2 ) return *this; m_id = S2.m_ID; m_lastname = S2.m_LastName; return *this; 33