CS-240 Operator Overloading Dick Steflik. Operator Overloading What is it? –assigning a new meaning to a specific operator when used in the context of.

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.
Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
Class and Objects.
Abstract Data Types Development and Implementation JPC and JWD © 2002 McGraw-Hill, Inc.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Chapter 13: Overloading.
Definition Class In C++, the class is the construct primarily used to create objects.
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 OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
Operator Overloading in C++
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
1 Operator Overloading in C++ Copyright Kip Irvine, All rights reserved. Only students enrolled in COP 4338 at Florida International University may.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes.
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
Operators & Overloading Joe Meehean. Expressions Expression composed of operands combined with operators e.g., a + b Operands variables and literals in.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
Case Study - Fractions Timothy Budd Oregon State University.
Operator Overloading & Exception Handling TCP1201 OOPDS 1 Lecture 5 1.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Operator Overloads Part2. Issue Provide member +(int) operator Rational + int OK int + Rational Error.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
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.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
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,
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
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.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Operator Overloading.
CSE1002 – Problem Solving with Object Oriented Programming
CSCE 210 Data Structures and Algorithms
Chapter 13: Overloading and Templates
Overloading Operator MySting Example
Chapter 15: Overloading and Templates
Accessor and Mutator Functions
Operator Overloading.
Advanced Program Design with C++
Operator Overloading; String and Array Objects
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
COMS 261 Computer Science I
COP 3330 Object-oriented Programming in C++
Development and Implementation
Operator Overloading; String and Array Objects
C++ support for Object-Oriented Programming
Object Oriented Programming
Presentation transcript:

CS-240 Operator Overloading Dick Steflik

Operator Overloading What is it? –assigning a new meaning to a specific operator when used in the context of a specific class ex. << is normally the shift left operator; but when used in conjunction with the class iostream it becomes the operator insertion; this is because the writers of the iostream class overloaded << to be insertion –cout << “some string” << endl; remember cout is a predefined object of class iostream –in another class, << could be overloaded to mean something.

Operator Overloading Why do we do it? –the c++ developers felt that it made more sense to overload an operator than to come up with some name for a function than meant the same thing as the operator. –using an overloaded operator takes fewer keystrokes –many people feel that an overloaded operator is more self documenting

Operator Overloading Three basic ways: –as a free function (not part of a class) –as a member function –as a friend function

Operator Overloading class Rational { Rational (int, int); const Rational & operator = (const Rational & rhs); const Rational & operator + (const Rational & rhs); bool operator == (const Rational & rhs); int getNumer() const { return numer; } int getDenom() const { return denom; } private: int numer; int denom; }

Overloading = as a member const Rational & Rational :: operator = (const Rational & rhs) { if (this != &rhs) { numer = rhs.numer; denom = rhs.denom; } return *this } Rational r1, r2; r1 = r2; -note: the if statement catches the special case where someone is trying r1=r1, the if statement makes the code faster

Overloading + as a member const Rational Rational::operator + (const Rational & rhs) { Rational answer ( *this); // initialize the answer with the current object answer += rhs; // add the second operand (assumes += has been overloaded) return answer; // return the answer via the copy constructor }

Overloading == as a free function // not defined in the Rational class bool operator == (const & Rational lhs, const & Rational rhs) { return (lhs.getDenom() * rhs.getNumer() == lhs.getNumer() * rhs.getDenom()); } Rational r1, r2; if ( r1 == r2 ) cout……… else cout

Overloading == as a member const bool Rational::operator == (const Rational & rhs ) { return ( numer * rhs.denom == denom * rhs.numer); } Rational r1, r2 ; if (r1 == r2 ) cout ……. else cout ……

Friend Functions not a member function has access to private data member functions work with the current (named object) friend functions work with multiple objects of the same class tag as a friend of the class –as part of class definition identify, by prototype, each friend of the class

Friend Functions (cont.) Friend functions are needed in C++ due to C++’s flawed object model, Java has a better model (all objects are derived from a single object). define the prototype in the public section of the class definition precede the prototype with the keyword “friend”

Friend Functions (more) define the friend implementation in the.cpp file with the member functions do not precede the function name with the class name and the scoping operator (ex. classname::)

Overloading == as a friend class Rational { Rational (int, int); const Rational & operator = (const Rational & rhs); const Rational & operator + (const Rational & rhs); friend bool operator == (const Rational & lhs, const Rational & rhs); private: int numer; int denom; } bool operator == (const Rational & lhs, const Rational & rhs) { return ( lhs.numer * rhs.denom == lhs.denom * rhs.numer); }

Overloading << as a friend class Rational { Rational (int, int); const Rational & operator = (const Rational & rhs); const Rational & operator + (const Rational & rhs); friend ostream & operator << (ostream & ostr, const Rational * rhs); private: int numer; int denom; } ostream & operator << (ostream & ostr, const Rational * rhs) { ostr << “numerator = “ << rhs.numer << “ denominator = “ << rhs.denom; }

More Overloading Thoughts = overload as a member function == != = overload as a member >> << (insertion and extraction) overload as non-members (friends) returning type iostream +-*/% (arithmetics) overload as members += -=... overload same as + and -

Please note: The only operators that cannot be overloaded are –. (dot operator) –.* (pointer-to-member) –sizeof –?: (three operands)

Only existing operators can be overloaded. new operators cannot be created (have to be made a named function member)