Operator Overloading Part 2

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - Operator Overloading Outline 8.1 Introduction 8.2 Fundamentals of Operator Overloading 8.3.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Operator Overloading Fundamentals
Operator Overloading. Basics Define operations on user-defined data types –Perform same operation as it is supposed to but, with operands of the user-defined.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
CMSC 2021 Stream I/O Operators and Friend Functions.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
1 Operator Overloading in C++ Copyright Kip Irvine, All rights reserved. Only students enrolled in COP 4338 at Florida International University may.
Object Oriented Programming in C++ Chapter5 Operator Overloading.
CMSC 202 Lesson 12 Operator Overloading II. Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private:
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.
Copyright  Hannu Laine C++-programming Part 4: Operator overloading.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
OPERATOR OVERLOADING WEEK 4-5 CHAPTER 19. class Money {private:int lira; int kurus; public: Money() {}; Money(int l, int k) { lira=l+ k/100; kurus=k%100;
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Friend Functions. Problem u Assuming two Complex objects u How would one add two numbers? W + X Complex operator+(const Complex& w, const Complex& x);
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Andy Wang Object Oriented Programming in C++ COP 3330
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading Introduction
Yan Shi CS/SE 2630 Lecture Notes
Chapter 13: Overloading and Templates
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Overloading Operator MySting Example
Object Oriented Programming COP3330 / CGS5409
Object-Oriented Design (OOD) and C++
Operator Overloading CMSC 202.
Object-Oriented Programming (OOP) Lecture No. 21
Chapter 15: Overloading and Templates
Friends and Unary Operators
Overloading the << operator
Operator Overloading; String and Array Objects
Operator Overloading.
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
Advanced Program Design with C++
CMSC 202 Lesson 22 Templates I.
Friends, Overloaded Operators, and Arrays in Classes
Operator Overloading, Friends, and References
Introduction to Programming
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
COP 3330 Object-oriented Programming in C++
Engineering Problem Solving with C++ An Object Based Approach
Templates I CMSC 202.
Operator Overloading; String and Array Objects
Eighth step for Learning C++ Programming
CMSC 202 Lesson 6 Functions II.
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
Overloading the << operator
C++ data types.
Presentation transcript:

Operator Overloading Part 2 CMSC 202

Recall Private/Public Any method or function from anywhere can access these Private Only class-methods can access these Is there a way to get around this? Yes!

In class implementation! Friends Have access to an object’s private methods and data Syntax: friend retType methodName(params); retType methodName(params) { /* code */ } In class declaration! In class implementation!

Friend vs. Non-friend Friend Non-friend Why would you want this? friend const Money operator+ (const Money& a, const Money& b); // in class const Money operator+ (const Money& a, const Money& b) { return Money( a.dollars + b.dollars, a.cents + b.cents); } Non-friend const Money operator+ (const Money& a, const Money& b); // NOT in class return Money( a.GetDollars() + b.GetDollars(), a.GetCents() + b.GetCents()); Why would you want this?

Input/Output Overload the insertion << and extraction >> operators Cannot be member functions (why?) Can be friends Because… Money m; cin >> m; cout << “My money: “ << m << endl; Is better than… m.Input(); cout << “My money: “; m.Output(); cout << endl;

Output – Insertion Operator << Non-friend ostream& operator<<( ostream& sout, const Money& money); // NOT in class const Money& money) { sout << “$” << money.GetDollars() << “.” << money.GetCents(); return sout; } Friend (don’t forget to add friend to the prototype!) friend ostream& operator<<( ostream& sout, const Money& money); // in class sout << “$” << money.dollars << “.” << money.cents;

Operator<< Notes… You should override << for all of your classes Do not include a closing endl (after all data…why?) Operator<< is not a member function Always return ostream& Why?

Input – Extraction Operator >> // Input money as X.XX // friend version… istream& operator>>(istream& sin, Money& money) { char dot; sin >> money.dollars >> dot >> money.cents; return sin; } How would you do this as a non-friend function?

Unary Operators Can we overload unary operators? Negation, Increment, Decrement? YES! Let’s look at two cases Negation Increment Pre and Post Example Money m1(3, 25); Money m2; m2 = - m1; ++m2; m1 = m2++;

Negation (member function) const Money operator- ( ) const; const Money Money::operator- ( ) const { Money result; result.m_dollars = -m_dollars; result.m_cents = -m_cents; return result; }

Pre Increment Money Money::operator++( void ) { // increment the cents ++m_cents; // adjust the dollars if necessary // return new Money object return Money( m_dollars, m_cents); }

Post Increment Money Money::operator++( int dummy ) { // make a copy of this Money object // before incrementing the cents Money result(m_dollars, m_cents); // now increment the cents ++m_cents; // code here to adjust the dollars // return the Money as it was before // the increment return result; }

Restrictions Can’t overload every operator Can’t make up operators Can’t overload for primitive types Like operator<< for integers… Can’t change precedence Can’t change associativity Like making (-m) be (m-)

Good Programming Practices Overload to mimic primitives Binary operators should Return const objects by value Be written as non-member functions Be written as non-friend functions Overload unary as member functions Always overload << As non-friend if possible Overload operator= if using dynamic memory

Practice Let’s overload the operator== for the Money class Should it be a member function? Should it be a friend? What should it return? What parameters should it have? What do we need to do inside?

Challenge Overload the operator+= for a Money object Should it be a member function? Should it be a friend? What should it return? What parameters should it have? What do we need to do inside?