Operator Overloading. 2 Objectives Discuss operator overloading –definition –use –advantages –limitations Present type conversion operators.

Slides:



Advertisements
Similar presentations
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Advertisements

ISBN Chapter 7 Expressions and Assignment Statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Encapsulation by Subprograms and Type Definitions
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
2 Objectives You should be able to describe: Operator Functions Two Useful Alternatives – operator() and operator[] Data Type Conversions Class Inheritance.
ISBN Chapter 7 Expressions and Assignment Statements.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator Overloading in C++
Review of C++ Programming Part II Sheng-Fang Huang.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Operator Overloading and Type Conversions
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
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.
C H A P T E R S E V E N Expressions and Assignment Statements.
Programming Language C++ Xulong Peng CSC415 Programming Languages.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Case Study - Fractions Timothy Budd Oregon State University.
Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Expressions and Assignment Statements
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. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
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.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
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.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Operator Overloading Week 5.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Expressions and Assignment Statements
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Operator Overloading; String and Array Objects
Expressions and Assignment Statements
C# Operator Overloading and Type Conversions
Constructor & Destructor
Expressions and Assignment Statements
Expressions and Assignment Statements
Arithmetic Expressions
Conversions of the type of the value of an expression
Operator Overloading
Operators Lecture 10 Fri, Feb 8, 2008.
Operator Overloading; String and Array Objects
Expressions and Assignment Statements
Andy Wang Object Oriented Programming in C++ COP 3330
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,
COP 3330 Object-oriented Programming in C++
Operator Overloading; String and Array Objects
Andy Wang Object Oriented Programming in C++ COP 3330
Unit-1 Introduction to Java
Operator King Saud University
Expressions and Assignment Statements
Presentation transcript:

Operator Overloading

2 Objectives Discuss operator overloading –definition –use –advantages –limitations Present type conversion operators

3 Arithmetic operations Could define a method to perform arithmetic operation –supply as part of class or struct struct Point { int x; int y; public static Point Add(Point p, Point q) { return new Point(p.x + q.x, p.y + q.y); }... } add points Point a = new Point(1, 2); Point b = new Point(3, 4); Point c = Point.Add(a, b); invoke Add

4 Operator overloading Can overload operators to work with class and struct types –use keyword operator –follow with symbol struct Point { int x; int y; public static Point operator+(Point p, Point q) { return new Point(p.x + q.x, p.y + q.y); }... } overload +

5 Using overloaded operator Overloaded operator used like operators for other types –compiler translates into method call Point a = new Point(1, 2); Point b = new Point(3, 4); Point c = a + b; use operator +

6 Advantages of operator overloading Operator overloading yields advantages for user code –concise –readable –takes advantage of user's existing knowledge of symbol Point a = new Point(1, 2); Point b = new Point(3, 4); Point c = a + b; Point d = Point.Add(a, b); operator method

7 Binary operators Binary operators take two parameters struct Point { int x; int y; public static Point operator+(Point p, Point q) { return new Point(p.x + q.x, p.y + q.y); } public static Point operator-(Point p, Point q) { return new Point(p.x - q.x, p.y - q.y); }... } binary + binary -

8 Unary operators Unary operators take single parameter struct Point { int x; int y; public static Point operator+(Point p) { return new Point(p.x, p.y); } public static Point operator-(Point p) { return new Point(-p.x, -p.y); }... } unary + unary -

9 Mixed types Can mix parameter types –separate method for each combination of parameter type/order struct Point { public static Point operator*(Point p, int a) { return new Point(p.x * a, p.y * a); } public static Point operator*(int a, Point p) { return new Point(a * p.x, a * p.y); }... } Point*int int*Point

10 Equality Can overload equality and inequality –should ensure Equals method has same semantics equality struct Point { public static bool operator==(Point p, Point q) { return p.x == q.x && p.y == q.y; } public static bool operator!=(Point p, Point q) { return !(p == q); }... } Point a = new Point(1, 2); Point b = new Point(3, 4); if (a == b)... compare points inequality

11 Operator pairs Some operators are required to be present in pairs –== and != –> and < –>= and <= –true and false equality struct Point { public static bool operator==(Point p, Point q) { return p.x == q.x && p.y == q.y; }... } error, must also provide inequality

12 Compound assignment Compound assignment operator provided automatically –when corresponding binary operator overloaded define binary + struct Point { public static Point operator+(Point p, Point q) { return new Point(p.x + q.x, p.y + q.y); }... } Point a = new Point(1, 2); Point b = new Point(3, 4); Point c; c = a + b; c += b; get operator + get operator +=

13 Method format Overloaded operator must be member of class or struct Must have specific modifiers –public –static struct Point { int x; int y; public static Point operator+(Point p, Point q) { return new Point(p.x + q.x, p.y + q.y); }... } required modifiers

14 Parameter types At least one parameter must be of enclosing type –prevents redefinition of operators on existing type struct Point { int x; int y; public static Point operator+(int x, int y) { return new Point(x, y); }... } error

15 Limitations Only some operators can be overloaded –unary: + - ! ~ true false –binary: + - * / % & | ^ > == != > = <= Cannot –create new operators –change precedence –change associativity –change number of arguments –overload prefix/postfix versions separately –pass parameters ref or out

16 Cross language Not all.NET languages support operator overloading –operators therefore not available to clients in all languages –should provide regular method in addition to operator struct Point { public static Point operator+(Point p, Point q) { return Add(p, q); } public static Point Add(Point p, Point q) { return new Point(p.x + q.x, p.y + q.y); }... } provide operator provide method

17 Type conversion operators Can overload the type conversion operators –implement user defined type converters –invoke automatically or using cast syntax Rational: 1/2double: 0.5 int: 3Rational: 3/1 Polar: (1, 3.14)Cartesian: (-1, 0)

18 sourcerequired destinationchoose one Converter syntax Define converter using keyword operator –operator name is destination type of conversion –parameter is source of conversion struct Cartesian {... public static operator Cartesian(Polar p) {... } } explicit implicit required

19 Implementing converter Converter should create and return object of destination type –using data in source struct Cartesian { int x; int y; public static explicit operator Cartesian(Polar p) { Cartesian c = new Cartesian(); c.x = p.r * Math.Cos(p.theta); c.y = p.r * Math.Sin(p.theta); return c; }... } create object of destination type convert data return new object

20 Converter uses Converter can be used whenever conversion required –assignment –parameter passing –method return

21 Explicit Explicit converters must be invoked using cast –safest choice –requires user to acknowledge type conversion with cast struct Cartesian { public static explicit operator Cartesian(Polar p) {... }... } explicit converter cast required Polar p = new Polar(1, Math.PI); Cartesian c = (Cartesian)p;

22 Implicit Implicit converter automatically used by compiler as needed –makes user code minimal –but can make code more difficult to understand –often recommended only if no information is lost in conversion struct Cartesian { public static implicit operator Cartesian(Polar p) {... }... } implicit converter no cast required Polar p = new Polar(1, Math.PI); Cartesian c = p;

23 Limitations Several limitations on conversion operators –must be public –must be static –can have only single parameter –parameter can not be passed ref or out –parameter or return type must be same as enclosing type

24 Summary C# allows many operators to be overloaded –many restrictions help avoid many tricky cases Can overload type conversion operators –create user defined type converter