Operator Overloads Part1

Slides:



Advertisements
Similar presentations
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Advertisements

Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Operator Overloading Fundamentals
Operator Overloading Back to Fractions.... Implementing an Object We’ve talked at length about object- orientation. – We’ve looked heavily at encapsulation.
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.
1 Operator Overloading. 2 Syntax The general syntax is: [friend] returntype operator ( ) { ; }
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
CMSC 202 Lesson 12 Operator Overloading II. Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private:
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
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.
ECE122 Feb. 22, Any question on Vehicle sample code?
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads.
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.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
C# Operator Overloading and Type Conversions C#.NET Software Development Version 1.0.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
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. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
A: A: double “4” A: “34” 4.
Const Member Functions Which are read-only? //fraction.h... class Fraction { public: void readin(); void print();
Rational Expressions relational operators logical operators order of precedence.
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
Chapter 18 - C++ Operator Overloading
Object-Oriented Programming (OOP) Lecture No. 16
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading CS 3370 – C++ Chapter 14.
Operator Overloading.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value A copy of the argument’s value.
Friend functions, operator overloading
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
C# Operator Overloading and Type Conversions
Section 3 Review Mr. Crone.
Haskell Chapter 2.
Agenda Warmup Finish 2.4 Assignments
CSC 205 Java Programming II
Object Oriented Programming COP3330 / CGS5409
Operator Overloading Part 2
Object-Oriented Programming (OOP) Lecture No. 21
Object-Oriented Programming (OOP) Lecture No. 16
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Friends and Unary Operators
Object Oriented Programming
Automatics, Copy Constructor, and Assignment Operator
Operator Overloading
Automatics, Copy Constructor, and Assignment Operator
نوع داده هاي انتزاعي Abstract Data Types
Arrays and Classes Programming in C++ Fall 2008
Operator Overloading; String and Array Objects
Advanced Program Design with C++
CMSC 202 Lesson 22 Templates I.
Defining Classes I Part A.
Copy Assignment CSCE 121 J. Michael Moore.
Operator Overloading.
COP 3330 Object-oriented Programming in C++
Operator Overloads Part1
COP 3330 Object-oriented Programming in C++
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Class rational part2.
Copy Assignment CSCE 121.
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Chapter 8 (Part 2) Destructor & Operator Overloading (P.331)
Presentation transcript:

Operator Overloads Part1

Rational Class RationalNumber (Fraction) class: Rational -numerator: int -denominator: int +Rational(numerator: int, denominator: int) +getNumerator(): int const +getDenominator(): int const +add(secondRational: Rational&): Rational const +subtract(secondRational: Rational&): Rational const +multiply(secondRational: Rational&): Rational const +divide(secondRational: Rational&): Rational const +compareTo(secondRational: Rational&): int const +equals(secondRational: Rational&): bool const +intValue(): int const +doubleValue(): double const +toString(): string const -gcd(n: int, d: int): int

Rational Use Currently awkward to use:

Rational Use What we want:

Operator Overloading Classes get = and . operators automatically May need to change = Can overload other operators + - / * == > etc… Can't Make new operators Change precedence Change how operators work on plain types

Overload as Member Overload as member function: r1 + r2 interpreted as r1.operator+(r2)

Overload as Member Implemented as member function:

Notes Parameter can be any type: Return type doesn't have to be same class:

Other Overloads Other math as members:

Other Overloads Relational as members:

Relational Implementation Compare this to other, return true or false

Unary Unary operators

Augmented Assignments Expected to change original Expected to return a reference to original object

Augmented Assignment Augmented assignments return current object as reference

Augmented Assignment Augmented assignments return current object as reference r3 numerator 1 denominator 3

Augmented Assignment Augmented assignments return current object as reference r3 numerator 11 denominator 15

Augmented Assignment Augmented assignments return current object as reference r3 r3 numerator 11 denominator 15

Augmented Assignment Augmented assignments return current object as reference r3 r3 numerator 63 denominator 45

Augmented Assignment Augmented assignments return current object as reference r3 r3 numerator 63 denominator 45

++i vs i++ Preincrement Postincrement int x = 10; int y = ++x; x = 11 Change x, use new value Copy x to use in rest of statement, change x

Pre-crements Preincrement/decrement Operator ++ or -- Return reference to original

Pre behavior Preincrement in action: r2 numerator 2 denominator 3

Pre behavior Preincrement in action: r2 numerator 5 denominator 3

Pre behavior Preincrement in action: r2 r2 numerator 5 denominator 3 temp numerator 5 denominator 3

Post-crements Postincrement/decrement Dummy int param to distinguish pre/post Don't use Return a new object with old value

Post behavior Postincrement in action: r2 numerator 2 denominator 3

Post behavior Postincrement in action: r2 numerator 2 denominator 3 temp numerator 2 denominator 3

Post behavior Postincrement in action: r2 numerator 5 denominator 3 temp numerator 2 denominator 3

Post behavior Postincrement in action: temp r2 numerator 5 denominator 3 temp2 numerator 2 denominator 3 temp numerator 2 denominator 3

Post behavior Postincrement in action: r2 numerator 5 denominator 3 temp2 numerator 2 denominator 3

Important Lesson x++ generally involves copy for objects Favor ++x over x++ for objects