Operator Overloading Back to Fractions.... Implementing an Object We’ve talked at length about object- orientation. – We’ve looked heavily at encapsulation.

Slides:



Advertisements
Similar presentations
Abstract Data Type Fraction Example
Advertisements

Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Chapter 14 Operator Overloading: What does + mean? Consider the code below. Note the multiple uses of “+”. #include using namespace std; int main() { int.
C++ Features and Constructs Ch. 3 except 3.2, 3.4, 3.9, 3.11.
Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
Class and Objects.
Abstract Data Type. COMP104 Slide 2 Summary l A class can be used not only to combine data but also to combine data and functions into a single (compound)
Programmer-defined classes Part 3. A class for representing Fractions public class Fraction { private int numerator; private int denominator; public Fraction(int.
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.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
COSC 1P03 Data Structures and Abstraction 4.1 Abstract Data Types The advantage of a bad memory is that one enjoys several times the same good things for.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Case Study - Fractions Timothy Budd Oregon State University.
Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.
Operator Overloading & Exception Handling TCP1201 OOPDS 1 Lecture 5 1.
Object Orientation “Thinking” Object Oriented, Further Constraints, and Documentation.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 5 An Array Class Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
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:
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.
11 Introduction to Object Oriented Programming (Continued) Cats.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Operator Overloading Week 5.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
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.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.
11 Introduction to Object Oriented Programming (Continued) Cats.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Overview Class Scope Review: Object parameters passed by value reference constant reference Friend function Overloading operator.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
1 Introduction to Object Oriented Programming Chapter 10.
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
Andy Wang Object Oriented Programming in C++ COP 3330
CSE1002 – Problem Solving with Object Oriented Programming
Regarding assignment 1 Style standards Program organization
CSCE 210 Data Structures and Algorithms
Exercise 1 – Datentypen & Variablen
Object Oriented Programming COP3330 / CGS5409
Andy Wang Object Oriented Programming in C++ COP 3330
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Introduction to C++ October 2, 2017.
C Basics.
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
The dirty secrets of objects
Chapter 14: More About Classes.
Andy Wang Object Oriented Programming in C++ COP 3330
Object Oriented Programming
Automatics, Copy Constructor, and Assignment Operator
Fraction Abstract Data Type
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
CS150 Introduction to Computer Science 1
COP 3330 Object-oriented Programming in C++
COP 3330 Object-oriented Programming in C++
COP 3330 Object-oriented Programming in C++
Class rational part2.
CMPE212 – Reminders Assignment 2 due next Friday.
Chapter 8 (Part 2) Destructor & Operator Overloading (P.331)
Presentation transcript:

Operator Overloading Back to Fractions...

Implementing an Object We’ve talked at length about object- orientation. – We’ve looked heavily at encapsulation and related concerns. We’ve started to implement a data structure for fractions. – Data structures are special classes/objects designed to organize data within a program.

Fractions As noted earlier, C++ does not have a fraction or rational data type or object class – This does not mean that we can’t create a class that provides this functionality! – Let's do it! – Get out laptops and bring up the code we started a couple weeks ago

Object Requirements Our core idea: we want a class of objects that behave like fractions – What are some of the implications of this? – What must we store/track? – What operations should be possible? – What conversions? – What about operators?

Object Requirements Our core idea: we want a class of objects that behave like fractions – Fractions have a numerator and a denominator – we must store these – We can assign, compare, add, subtract, multiply, and divide fractions – It would be nice to convert a fraction to an int or a double. How about the reverse directions? – What about strings?

Object Requirements Our core idea: we want a class of objects that behave like fractions – Converting to int or double is easy. – Converting from int is easy – Converting from double... … not so much! – Converting to a string is easy... … from a string a bit harder

Object Requirements Our core idea: we want a class of objects that behave like fractions – What about binary operators like +, -? – We can overload these just like functions! – How about +=, -=, etc.? Yep! – How about =,, etc.? Yep, those too!

Object Requirements We need constructors and destructors We may also need getters and setters for numerator and denominator We can also provide for “automatic” type conversion!

Implementing Our Object Step 1: determining the declarations – These go in the header (Fraction.h) file. class Fraction { private: int numerator; int denominator; public: Fraction(int, int); Fraction(); // more later

Implementing Our Object Step 1 (con't): more declarations Fraction add(const Fraction &f) const; Fraction subtract(const Fraction &f) const; Fraction multiply(const Fraction &f) const; Fraction divide(const Fraction &f) const; // comparison methods int compare(const Fraction &f) const; bool equals(const Fraction &f) const; // conversion methods int intValue() const; double doubleValue() const; string toString() const;

Implementing Our Object Step 1(con't): more declarations Fraction add(const Fraction &f) const; Fraction subtract(const Fraction &f) const; Fraction multiply(const Fraction &f) const; Fraction divide(const Fraction &f) const; int compare(const Fraction &f) const; bool equals(const Fraction &f) const; int intValue() const; double doubleValue() const; string toString() const; Note the use of const here - This means that the method will NOT change the object on which it is called

Implementing Our Object Step 1(con't): more declarations Fraction add(const Fraction &f) const; Fraction subtract(const Fraction &f) const; Fraction multiply(const Fraction &f) const; Fraction divide(const Fraction &f) const; int compare(const Fraction &f) const; bool equals(const Fraction &f) const; int intValue() const; double doubleValue() const; string toString() const; Note also that we are returning the object itself rather than a pointer to the object as before (no *) Use of string means we must #include

Implementing Our Object Step 2: implementing the methods (goes in Fraction.cpp file). Fraction::Fraction() : numerator(0), denominator(1) {} Fraction::Fraction(int n, int d) { numerator = n; denominator = d; } Wouldn't it be nice if the fraction were in reduced form? Solution: implement a private gcd() function in Fraction, use it to reduce form

Implementing Our Object Revised version of constructor: Fraction::Fraction() : numerator(0), denominator(1) {} Fraction::Fraction(int n, int d) { int g = gcd(n, d); if (g > 1) { n /= g; d /= g; } numerator = n; denominator = d; }

Implementing Our Object What does gcd look like? int Fraction::gcd(int n, int d) { int n1 = abs(n); // want these positive int n2 = abs(d); int gcd = 1; for (int k = 1; k <= n1 && k <= n2; ++k) { if (n1 % k == 0 && n2 % k == 0) gcd = k; } return gcd; } Note: this is a cheesy implementation of the gcd function! Remember to put prototype in.h file as private, static! Oh yeah! And be sure to #include in Fraction.cpp file!

Implementing Our Object Step 2: OK – constructors done, now for addition, etc. (we did this one before, 'member?) Fraction Fraction::add(const Fraction &f) const { int num = this->numerator * f.denominator; num += f.numerator * this->denominator; int dnm = f.denominator * denominator; return Fraction(num, dnm); }

Implementing Our Object Step 2: … subtraction, multiplication,... Pretty straight-forward… Fraction Fraction::subtract(const Fraction &f) const { int num = this->numerator * f.denominator; num -= f.numerator * this->denominator; int dnm = f.denominator * denominator; return Fraction(num, dnm); }

Implementing Our Object Step 2: … multiplication, division,... Fraction Fraction::multiply(const Fraction &f) const { int num = this->numerator * f.numerator; int dnm = this->denominator * f.denominator; return Fraction(num, dnm); }

Implementing Our Object Step 2: … division,... Fraction Fraction::divide(const Fraction &f) const { // divide is multiply by reciprocal int num = this->numerator * f.denominator; int dnm = this->denominator * f.numerator; return Fraction(num, dnm); }

Implementing Our Object Step 2: Now for comparison... int Fraction::compare(const Fraction &f) const { Fraction temp = subtract(f); // difference int num = temp.getNum(); if (num smaller return (num > 0 ? 1 : 0); // pos => bigger } bool Fraction::equals(const Fraction &f) const { return(0 == compare(f)); }

Implementing Our Object Step 2: Conversion to built-ins... Easy peasy int Fraction::intValue() const { return (numerator)/(denominator); } double Fraction::doubleValue() const { return ((double) numerator )/((double) denominator); }

Implementing Our Object Step 2: … and conversion to string string Fraction::toString() const { stringstream ss; ss << numerator; if (denominator > 1 && numerator != 0) ss << "/" << denominator; return (ss.str()); } What is the if for??? Prevent output like 3/1 and 0/4

Testing Our Class Step 3: Writing some test code (This goes in testFraction.cpp) #include #include "Fraction.h" using namespace std; int main() { int i; int j; Fraction g1;...

Testing Our Class Step 3: Writing test code... Fraction g1; cout << "Enter two integers: " << flush; cin >> i >> j; g1.setNum(i); // test setters g1.setDenom(j); cout << "Enter two integers: " << flush; cin >> i >> j; Fraction g2(i,j); // test list constructor...

Testing Our Class Step 3: Writing test code cout << g1.toString() // test toString << " plus " << g2.toString() << " equals " << g1.add(g2).toString() // test add << endl;

Testing Our Class Step 3: etc. etc. etc. for other 3 fcns, then do compare: cout << g1.toString() << " compare " << g2.toString() << " is " << g1.compare(g2) << endl;

Testing Our Class Step 3: and equals: cout << g1.toString() << " equals " << g2.toString() << " is " << g1.equals(g2) << endl; Return 0; // done for now! }

Exercise 1: Implement Fraction class (you should have most of this from before) – just constructors, setters, add(), toString(), and compare() for now... Use direct form rather than pointer version Compile – no need to have these as 3 files for now... check #includes!!! Run test and see that it works

Review Requirements We have met all the basic requirements  Arithmetic operations, comparison, conversion to float, int, and string Missing conversion from int, float, or string What about operators? And automatic conversion?

Conversion from int Automatic (compiler) conversion from other types can occur when a function is called (such as add) that needs another Fraction, but an int or float is the actual parameter Compiler will first search for overloaded function with matching signature – not there! Then it looks for … constructor!

Conversion from int Step 1: More declarations class Fraction { private: int numerator; int denominator; static int gcd(int n, int d); public: Fraction(int, int); Fraction(); Fraction(int n); // conversion from int...

Conversion from int Step 2: implementation Fraction::Fraction(int n) { numerator = n; denominator = 1; } So what about floats? Hmmmmmm....

Overloading Operators Wouldn't it be nice to be able to use code like if (f1 > f2) { … } Well, C++ allows this! In fact, C++ allows lots of operators to be overloaded Use the special operator function Named with operator keyword followed by the actual operator

Overloading Operators Step 1: More declarations class Fraction { private: int numerator; int denominator; static int gcd(int n, int d); public:... bool operator<(const Fraction& f) const; bool operator==(const Fraction& f) const;... operator keyword Operator symbol(s)

Overloading Operators Step 2: implementation bool Fraction::operator<(const Fraction& f) const { return (compare(f) < 0); } bool Fraction::operator==(const Fraction& f) const { return (compare(f) == 0); }

Testing Operator Overload Step 3: Writing some test code... // test overloading < operator cout << g1.toString() << " < " << g2.toString() << " is " << (g1 < g2) << endl; // now test auto conversion from int cout << g1.toString() << " plus 1 equals " << g1.add(1).toString() << endl;...

Exercise 2: Add declarations and implementation for integer conversion and overloading < operator Add a little code to test these Compile Run test and see that it works

More Operators Step 1: More declarations class Fraction {... public:... Fraction operator+(const Fraction& f) const; Fraction operator-(const Fraction& f) const; Fraction operator*(const Fraction& f) const; Fraction operator/(const Fraction& f) const;... operator keyword Operator symbol(s)

More Operators Step 2: implementation Fraction Fraction::operator+(const Fraction& f) const { return (add(f)); } Fraction Fraction::operator-(const Fraction& f) const { return (subtract(f)); }

Augmented Assignment What about code like f1 += f2; Well, C++ also allows this! Issue here is that assignment also returns a Lvalue... how to solve? So declare as reference Fraction& and return object Can't use const any more!!! Why not?

AA Operators Step 1: More declarations class Fraction {... public:... Fraction& operator+=(const Fraction& f) ; Fraction& operator-=(const Fraction& f) ; Fraction& operator*=(const Fraction& f) ; Fraction& operator/=(const Fraction& f) ;... reference type return value No more const!

More Operators Step 2: implementation Fraction& Fraction::operator+=(const Fraction& f) { *this = this->add(f); return *this; } reference return type so it can be Lvalue Modify object Return modified object

More Testing Overload Step 3: Writing some test code... cout << g1.toString() << " plus equal " << g2.toString() << " equals "; g1 += g2; cout << g1.toString() << endl;...

Exercise 3: Add declarations and implementation for overloading + and += operators Add a little code to test these Compile Run test and see that it works

Operator Overloading Can also overload [] indexing operator – see lab Only operators that can't be overloaded in C++ are: ?:..* :: Only overload operators when the overloaded function fulfills the logical function of the original operator!

Questions? Project 4: Implement overload operators for set and multiset: + (union), - (subtraction), +=, -=, * (intersect), ^ (difference), *=, ^=, == (equality), < (proper subset), <= (subset)