Operator Overloading Week 5.

Slides:



Advertisements
Similar presentations
Generics, Lists, Interfaces
Advertisements

Intermediate Code Generation
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Types and Arithmetic Operators
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
Operator Overloading Back to Fractions.... Implementing an Object We’ve talked at length about object- orientation. – We’ve looked heavily at encapsulation.
Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown,
1 CS2200 Software Development Lecture 27: More Testing A. O’Riordan, 2008 K. Brown,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
2 Objectives You should be able to describe: Operator Functions Two Useful Alternatives – operator() and operator[] Data Type Conversions Class Inheritance.
Operator Overloading. 2 Objectives Discuss operator overloading –definition –use –advantages –limitations Present type conversion operators.
Programmer-defined classes Part 3. A class for representing Fractions public class Fraction { private int numerator; private int denominator; public Fraction(int.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator Overloading in C++
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
CS 363 Comparative Programming Languages Expressions and Assignment Statements.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CPS120: Introduction to Computer Science
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.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
Operator Overloads Part2. Issue Provide member +(int) operator Rational + int OK int + Rational Error.
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+
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Mathematical Calculations in Java Mrs. G. Chapman.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
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.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
C# Operator Overloading and Type Conversions C#.NET Software Development Version 1.0.
Object Oriented Programming
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Operators and Cast. Operators Operator Shortcuts.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Operator Overloading.
Chapter 7: Expressions and Assignment Statements
C# Operator Overloading and Type Conversions
Java Primer 1: Types, Classes and Operators
Chapter 7: Expressions and Assignment Statements
Objective The student will be able to:
Operator Overloading BCA Sem III K.I.R.A.S.
Chapter 2.
Introduction to C++ Programming
Coding Concepts (Data- Types)
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CISC/CMPE320 - Prof. McLeod
COP 3330 Object-oriented Programming in C++
Java Programming Language
CS 240 – Advanced Programming Concepts
Presentation transcript:

Operator Overloading Week 5

Operator Overloading There are various C# built-in types, such as integer (int) and Boolean (bool), and there are also various operators that allow these data types to be manipulated arithmetic: + - * / % comparisons: == != < <= > >= logical: && || ! & | ^ Using most of these operators with basic types is simple and intuitive Using these operators with created classes will cause errors

Designing the Fraction Class Create a new type called Fraction to store fractional numbers: Fraction first = new Fraction(1, 2); //create ½ Fraction second = new Fraction(3, 4); //create ¾ Constructors take the first parameter as the numerator, and the second parameter as the denominator To give the Fraction class all the functionality of the built-in types, there will be the need to perform arithmetic on instances of Fraction Add two Fractions, multiple them, etc Conversions to or from built-in types should also be available

It would be much better to be able to write: Each of these operations could be implemented using methods, but it’s not intuitively obvious: Fraction sum = first.Add(second); This is hard to read It’s not how a user would immediately expect addition to work It doesn’t look like addition It would be much better to be able to write: Fraction sum = first + second; This is intuitive and easy to use It is consistent with how built-in operators are added

Using the operator Keyword C# allows methods and constructors to be overloaded It also enables most operators to be overloaded as well to make them sensitive to the context in which they are used The syntax is to write the keyword operator followed by the operator to overload Operators are public and static methods The return value of an operator represents the result of an operation The operator’s parameters are the operands

Define an addition operator for a Fraction class the same as any other method but with a difference: Instead of a method name, use operator+ Addition is a binary operation, requiring two operands, so the operator+ method will require two parameters of type Fraction Addition has a result, requiring a return value of type Fraction public static Fraction operator +(Fraction lhs, Fraction rhs) The parameters of lhs and rhs stand for left-hand side and right-hand side

public class Fraction { private int numerator; private int denominator; //create a fraction by passing in the numerator and denominator public Fraction(int numerator, int denominator) this.numerator = numerator; this.denominator = denominator; } //return a string representation of the fraction public override string ToString() string s = numerator.ToString() + "/" + denominator.ToString(); return s;

//overloaded + takes two fractions and returns their sum public static Fraction operator +(Fraction lhs, Fraction rhs) { //like fractions (shared denominators) can be added by adding their numerators if(lhs.denominator == rhs.denominator) return new Fraction(lhs.numerator + rhs.numerator, lhs.denominator); //simplistic solution for unlike fractions is to cross-multiply: // 1/2 + 3/4 == ((1*4) + (3*2)) / (2*4) == 10/8 //this method does not reduce int firstProduct = lhs.numerator * rhs.denominator; int secondProduct = rhs.numerator * lhs.denominator; return new Fraction(firstProduct + secondProduct, lhs.denominator * rhs.denominator); }

Output would be: public class Tester { public void Run() Fraction f1 = new Fraction(3, 4); Console.WriteLine("f1: {0}", f1); Fraction f2 = new Fraction(2, 4); Console.WriteLine("f2: {0}", f2); Fraction f3 = f1 + f2; //test the overloaded addition operator Console.WriteLine("f1 + f2 = f3: {0}", f3); } public static void Main() Tester t = new Tester(); t.Run(); Output would be: f1: 3/4 f2: 2/4 f1 + f2 = f3: 5/4

Creating Useful Operators Operator overloading can make code more intuitive and allow them to be used similar to built-in types However, they way they are used should make sense Operators that don’t naturally follow their traditional meanings will be confusing Use operator overloading when it makes an application clearer than accomplishing the same operations with explicit method calls

Overload operators to perform the same function or similar functions on class objects as the operators perform on objects of simple types At least one argument of an overloaded operator method must be a reference to an object of the class in which the operator is overloaded This prevents programmers from changing how operators work on simple types

The Equals Operator It’s very common to overload the == operator to determine whether two objects are equal What “equal” means is up to the programmer, although the criteria should be reasonable Two Employee objects might be equal if they have the same name or perhaps if the same employee ID Overloading the == operator works the same way as overloading any other operator public static bool operator ==(Fraction lhs, Fraction rhs) The == operator always returns a Boolean value (true or false)

C# insists that if the equals operator is overloaded, then the not-equals (!=) operator must also be overloaded It’s good programming practice to have the inequality operator delegate its work to the equality operator The != operator will return the opposite of the value of the == operator Similarly, the less than (<) and greater than (>) operators must be paired as must the less than or equal to (<=) and greater than or equal to (>=) operators

The Object class offers a virtual method called Equals() If the equals operator is overloaded, it is recommended that the Equals() method also be overridden Overriding the Equals() method allows the class to be compatible with other .NET languages that do not overload operators but still support method overloading That way, if the == operator can’t be used, the Equals() method is still available public virtual bool Equals(object o) Ensure that one Fraction object is being compared with another Fraction object, otherwise they cannot be equal

//add these methods to the existing Fraction class //test whether two Fractions are equal public static bool operator ==(Fraction lhs, Fraction rhs) { if(lhs.denominator == rhs.denominator && lhs.numerator == rhs.numerator) return true; //code here to handle unlike Fractions return false; } //delegates to operator == public static bool operator !=(Fraction lhs, Fraction rhs) return !(lhs == rhs); //just the opposite of == //tests for same types, then delegates public override bool Equals(object o) if(!(o is Fraction)) //objects have to be the same type to be equal return this == (Fraction)o; //cast the object as a Fraction and delegate to ==

Generates this output: //can now add to the Run() method Fraction f4 = new Fraction(5, 4); if(f4 == f3) Console.WriteLine("f4: {0} == f3: {1}", f4, f3); if(f4 != f2) Console.WriteLine("f4: {0} != f2: {1}", f4, f2); if(f4.Equals(f3)) Console.WriteLine("{0}.Equals({1})", f4, f3); Generates this output: f4: 5/4 == f3: 5/4 f4: 5/4 != f2: 2/4 5/4.Equals(5/4)

Conversion Operators C# will convert an int to a long implicitly, but will only allow a long to be converted to an int explicitly The conversion from int to long is implicit because any int will fit into a long Conversion from long to int must be explicit because it is possible to lose information in the conversion It would be useful for Fraction objects to be converted to intrinsic types (such as int) and back Given an int, implicit conversion to a Fraction can be supported because any whole value is equal to that value over 1 (15 == 15/1)

Given a Fraction, an explicit conversion back to an integer might be provided with the understanding that some information might be lost The Fraction 9/4 might be converted to the integer value 2 (truncating to the nearest whole number) To implement the conversion operator, the keyword operator is still used, but instead of a symbol to override, the type to be converted to is used instead public static implicit operator Fraction(int theInt) Use the implicit operator when the conversion is guaranteed to succeed and no information will be lost, otherwise, use the explicit operator instead

//add these methods to the existing Fraction class //overload the constructor to create a fraction from a whole number public Fraction(int wholeNumber) { numerator = wholeNumber; //any whole number over 1 is a Fraction denominator = 1; } //converts ints to Fractions implicitly public static implicit operator Fraction(int theInt) return new Fraction(theInt); //invoke constructor for a new Fraction //convert Fractions to ints explicitly public static explicit operator int(Fraction theFraction) return theFraction.numerator / theFraction.denominator; //return an int

Generates this output: //can now add to the Run() method Fraction f5 = f4 + 5; //implicit conversion of 5 to a Fraction Console.WriteLine("f4 + 5 = f5: {0}", f5); int truncated = (int)f5; //explicit conversion of f5 to an int Console.WriteLine("When you truncate f5 you get {0}", truncated); Generates this output: f4 + 5 = f5: 25/4 When you truncate f5 you get 6 The cast from an int to a Fraction is perfectly safe, so it can be implicit The implementation of this is to create a new Fraction object and to return it The implicit cast operator causes the constructor to be invoked