CMSC 202 Lesson 12 Operator Overloading II. Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private:

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.
1 Operator Overloading. 2 Syntax The general syntax is: [friend] returntype operator ( ) { ; }
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
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.
Operator Overloading 1. Introduction Let’s define a class for Complex numbers: class Complex { private: double real, image; public: Complex () : real(0.0),
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
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.
Operators & Overloading Joe Meehean. Expressions Expression composed of operands combined with operators e.g., a + b Operands variables and literals in.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Operator Overloading ~ and User Defined Conversions.
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:
Chapter 8 Operator Overloading, Friends, and References.
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.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
CS Object Oriented Programming Using C++
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;
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
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 – .
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
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);
Class Operations Creating New Types. Review Last time, we began building, a class to allow us to model temperatures: Last time, we began building Temperature,
Operator Overloading What is operator overloading? Most predefined operators (arithmetic, logic, etc.) in C++ can be overloaded when applied to objects.
Operator Overloading.
Andy Wang Object Oriented Programming in C++ COP 3330
CSE1002 – Problem Solving with Object Oriented Programming
Yan Shi CS/SE 2630 Lecture Notes
Chapter 13: Overloading and Templates
Object-Oriented Design (OOD) and C++
Operator Overloading Part 2
Operator Overloading CMSC 202.
Chapter 15: Overloading and Templates
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
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
CMSC 202 Lesson 6 Functions II.
Presentation transcript:

CMSC 202 Lesson 12 Operator Overloading II

Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private: vector passengers; };

Review of Private/Public  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!

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 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 const Money operator+ (const Money& a, const Money& b) { return Money(a.GetDollars() + b.GetDollars(), a.GetCents() + b.GetCents()); } Why would you want this?

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

Output – Insertion Operator <<  Non-friend ostream& operator<<( ostream& sout, const Money& money);// NOT in class ostream& operator<<( ostream& sout, 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 ostream& operator<<( ostream& sout, const Money& money) { sout << “$” << money.dollars << “.” << money.cents; return sout; }

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?