Operators Overloading

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Object Oriented 33 MCSD DORON AMIR
ממיבחניםC שאלות ++.
Module 12: Operators, Delegates, and Events. Overview Introduction to Operators Operator Overloading Creating and Using Delegates Defining and Using Events.
מבוא למדעי המחשב לתעשייה וניהול
בתרגול הקודם הורשה: –ניתן להרחיב רק מחלקה אחת –כל מה שלא private – עובר בהורשה –המילה השמורה super –יצירת היררכיה –Object היא שורש ההיררכיה –דריסה אופרטור.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
1 תוכנה 1 תרגול 14 – סיכום. 2 קצת על מנשקים מנשק יכול להרחיב יותר ממנשק אחד שירותים במנשק הם תמיד מופשטים וציבוריים public interface MyInterface { public.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב'
חורף - תשס " ג DBMS, צורות נורמליות 1 צורה נורמלית שלישית - 3NF הגדרה : תהי R סכמה רלציונית ותהי F קבוצת תלויות פונקציונליות מעל R. R היא ב -3NF.
תרגול חזרה. מבנה האובייקט תאר את מבנה האובייקט כולל מבנה טבלאות הפונקציות הוירטואליות עבור התכנית הבאה struct A { int x; virtual void a() {}; }; struct.
1 מבוא למדעי המחשב ביטויים. 2 ביטויים expressions ביטויים (expressions) הינם יצורים תחביריים בעלי טיפוס וערך. הגדרה אינדוקטיבית של ביטויים : קבועים הם.
חלון הפקודות מיועד לבצע פעולה אחת בכל פעם. כיצד אפשר לבצע רשימת פקודות או אפליקציות מורכבות ?
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב' Templates תבניות.
מבוא למדעי המחשב תרגול 3 שעת קבלה : יום שני 11:00-12:00 דוא " ל :
Last time on Clang משתנה: "פתק" המשמש את המחשב לשמירת מידע. לכל משתנה יש שם וטיפוס כללים לשמות משתנים –חייבים להכיל רק אותיות, מספרים ו '_' –חייבים להתחיל.
תרגול 5: ביטויים לוגיים ומשפטי תנאי (חזרה והרחבה)
מבוא למדעי המחשב, סמסטר א ', תשע " א תרגול מס ' 1 נושאים  הכרת הקורס  פסאודו - קוד / אלגוריתם 1.
Safari On-line books. מה זה ספארי ספארי זו ספריה וירטואלית בנושא מחשבים היא כוללת יותר מ כותרים כל הספרים הם בטקסט מלא ניתן לחפש ספר בנושא מסוים.
Operator Overloading. 2 Objectives Discuss operator overloading –definition –use –advantages –limitations Present type conversion operators.
11 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 1 Introduction to Programming in C תרגול
המשך תכנות מונחה עצמים 1. היום בתרגול  הורשה  שיטות מיוחדות  פולימורפיזם 2.
Logical OperatorstMyn1 Logical Operators Using logical operators, we can combine a series of comparisons into a single expression. if(letter>=‘A’ && letter
Methods public class Demonstrate { public static void main (String argv[]) { public static void main (String argv[]) { int script = 6, acting = 9, directing.
1 המרכז להוראת המדעים © האוניברסיטה העברית בירושלים עוברים לג ' אווה.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
C# Operator Overloading and Type Conversions C#.NET Software Development Version 1.0.
Operator Overloading Week 5.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
1 המרכז להוראת המדעים © האוניברסיטה העברית בירושלים פולימורפיזם מתקדם ממשקים בג ' אווה 1.
1 תרגול 11: Design Patterns ומחלקות פנימיות אסף זריצקי ומתי שמרת 1 תוכנה 1.
1 נתבונן בפונקציה הבאה public static int min(int[] a,int n) { int min = a[0]; for (int i = 1; (i < n ) && (i < a.length) ; i++) if (min > a[i]) min = a[i];
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan Introduction and basics of Java Slides adapted from Steven Roehrig.
Overview of C#. Agenda Structure of a C# Program Basic Input/Output Operations Compiling, Running, and Debugging Namespaces.
מחרוזות – הטיפוס String
האוניברסיטה העברית בירושלים
Operator Overloading.
Object Oriented Programming
Chapter 7: Expressions and Assignment Statements
Object Oriented Programming
Operators and Expressions
C# Operator Overloading and Type Conversions
Chapter 7: Expressions and Assignment Statements
Object Oriented Programming
Object Oriented Programming
מחלקות classes.
Chapter 2.
תוכנה 1 תרגול 13 – סיכום.
תוכנה 1 תרגול 13 – סיכום.
תכנות מונחה עצמים.
Implementing Polymorphism
SQL בסיסי – הגדרה אינדוקטיבית
בניית מחלקות.
בניית מחסנית סטטית Static Stack Implementation מורים מובילים תשע"ה
בניית מחלקות.
תרגול 10 המשך תכנות מונחה עצמים.
תוכנה 1 תרגול 13 – סיכום.
תכנות מכוון עצמים ו- C++ יחידה 02 העמסת פונקציות, ערכי ברירת מחדל, enum, קימפול מותנה קרן כליף.
תוכנה 1 תרגול 13 – סיכום.
פולימורפיזם מתקדם ממשקים בC# עריכה ועיצוב: קרן הרדי
class PrintOnetoTen { public static void main(String args[]) {
Control Structure Chapter 3.
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Control Structure.
Presentation transcript:

Operators Overloading

Agenda Introduction to Operator Overloading. Overloading Mathematical Operators. Overloading Logical Operators. Overloading Conversion Operators. Overloading Unary Operators. Overloading Operators Multiple Times.

Agenda Introduction to Operator Overloading. Overloading Relational Operators. Overloading Mathematical Operators. Overloading Conversion Operators. Overloading Unary Operators. Overloading Operators Multiple Times.

Introduction to Operator Overloading הפעולות הבאות ברורת מאליהן: int num1 = 12; int num2 = 23; int num3 = num1+num2; if(num1 == num2) num3 +=5; הפעולות הבאות יעוררו תמיהה: Date d1 = new Person(...) Date d2 = new Person(...) Date d3 = d1-d2; if(d1 == d2) d3 += 5;

Introduction to Operator Overloading פעילות אריתמטית לוגית על משתנים פשוטים היא דבר ברור, פעילות זו על אובייקטים מעוררת תמיהה, הכיצד ידע האופרטור כיצד לבצע את פעולתו? הכיצד ידע לאיזה שדה להוסיף ערך?, או על פי אילו שדות לבצע השוואה? כברירת מחדל האופרטורים אינם יודעים כיצד לפעול עם אובייקטים, אולם #C (כמו שפת ++C ) מסוגלת "ללמד" אותם. בהעמסה יש להקפיד על מספר כללים: נעמיס אופרטור רק במידה ויש משמעות ברורה מאליה להעמסה. נקפיד לשמור על כל המשמעויות המקוריות שלהאופרטור.

Introduction to Operator Overloading Syntax : public static Time operator+(Time t1, Time t2) { int newHours = t1.hours + t2.hours; int newMinutes = t1.minutes + t2.minutes; return new Time(newHours, newMinutes); } כל המתודות המעמיסות אופרטורים הן סטאטיות. מספר הפרמטרים שהמתודות מקבלות הוא כמספר האופרנדים. שם המתודה מתחיל במילה השמורה operator ולאחריה שם האופרטור המועמס.

Introduction to Operator Overloading האופרטורים הניתנים להעמסה unary operators +, -, !, ~, ++, --, true, false binary operators +, -, *, /, %, &, |, ^, <<, >> comparison operators ==, !=, <, >, <=, >= אולם הם חייבים להיות מועמסים בזוגות: == עם =! , > עם < , =< עם => .

Introduction to Operator Overloading האופרטורים שאינם ניתנים להעמסה +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= =, ., ?:, ->, new, is, sizeof, typeof, &&, ||

Introduction to Operator Overloading המטרה בהעמסת אופרטורים היא לשפר את הקריאות של הקוד ובכך להימנע משגיאות וטעויות. לדוגמה: ללא אופרטורים myIntVar1 = Int.Add(myIntVar2, Int.Add(Int.Add(myIntVar3, myIntVar4), 33)); עם אופרטורים : myIntVar1 = myIntVar2 + myIntVar3 + myIntVar4 + 33;

Agenda Introduction to Operator Overloading. Overloading Relational Operators. Overloading Mathematical Operators. Overloading Conversion Operators. Overloading Unary Operators. Overloading Operators Multiple Times.

Overloading Relational Operators מחייבים העמסה בזוגות: < and > <= and >= == and != בהעמסת האופרטורים == ו- != יש לדרוס את המתודות Equals ואת GetHashCode .

Overloading Relational Operators לדוגמה: class Circle { public static bool operator==( Circle c1,Circle c2) return c1.Equals(c2); } . . .

Overloading Relational Operators לדוגמה: class Circle { public static bool operator!=(Circle c1,Circle c2) if (c1.x != c2.x || c1.y != c2.y || c1.radius != c2.radius) return true; return false; } . . .

Agenda Introduction to Operator Overloading. Overloading Relational Operators. Overloading Mathematical Operators. Overloading Conversion Operators. Overloading Unary Operators. Overloading Operators Multiple Times.

Overloading Mathematical Operators ניתן להעמיס את האופרטורים + , - , * , / : class Circle { public static Circle operator+(Circle c1,Circle c2) Circle c = new Circle(); c.x = c1.x + c2.x; c.y = c1.y + c2.y; c.radius = c1.radius + c2.radius; return c; }

Overloading Mathematical Operators ניתן להעמיס את האופרטורים + , - , * , / : class Circle { . . . public static Circle operator-(Circle c1,Circle c2) Circle c = new Circle(c1.x - c2.x, c1.y - c2.y, c1.radius - c2.radius); return c; }

Agenda Introduction to Operator Overloading. Overloading Relational Operators. Overloading Mathematical Operators. Overloading Conversion Operators. Overloading Unary Operators. Overloading Operators Multiple Times.

Overloading Conversion Operators C# מאפשרת להגדיר מתודות העמסה מאוד מיוחדות User Defined Conversions. מתודות אלו מאפשרות לבצע המרה (Casting) מטיפוס לטיפוס באופן מפורש (Explicit) או מרומז (Implicit) על פי הגדרתנו.

Overloading Conversion Operators דוגמה: class Person { ... public static implicit operator string (Person p) { string str; str = p.name + " " + p.age; return str; } public static explicit operator Person (string str) Person p = new Person(); string []tmp = new string [2]; tmp = str.Split(char.Parse(" ")); p.name = tmp[0]; p.age = int.Parse(tmp[1]); return p;

Overloading Conversion Operators דוגמה, השימוש ב- Implicit : class App { static void Main(string[] args) Person p1 = new Person("Moshe",12); Console.WriteLine(p1); Person p2 = new Person(); . . . }

Overloading Conversion Operators דוגמה, השימוש ב- Explicit : class App { static void Main(string[] args) . . . Person p2 = new Person(); string str = "Shoshana 12"; p2 = (Person)str; Console.WriteLine(p2); }

Agenda Introduction to Operator Overloading. Overloading Relational Operators. Overloading Mathematical Operators. Overloading Conversion Operators. Overloading Unary Operators. Overloading Operators Multiple Times.

Overloading Unary Operators. אופרטורים אונאריים, אופרטורים בעלי אופרנד יחיד, כגון: ++, -- וכו' class Circle { . . . public static Circle operator++(Circle c) c.radius = c.radius + 1; return c; }

Overloading Unary Operators. האופרטורים true ו- false : class Circle { . . . public static bool operator true(Circle c) if (c.x > 0 || c.x < 100 || c.y > 0 || c.y < 100) return true; return false; } public static bool operator false(Circle c)

Agenda Introduction to Operator Overloading. Overloading Relational Operators. Overloading Mathematical Operators. Overloading Conversion Operators. Overloading Unary Operators. Overloading Operators Multiple Times.

Overloading Operators Multiple Times ניתן להעמיס את המתודות המעמיסות אופרטורים: public static Time operator+(Time t1, int hours) {...} public static Time operator+(Time t1, float hours) public static Time operator-(Time t1, int hours) public static Time operator-(Time t1, float hours)

Agenda Introduction to Operator Overloading. Overloading Relational Operators. Overloading Mathematical Operators. Overloading Conversion Operators. Overloading Operators Multiple Times.

Books Corner – הוצאת ספרים מקוונת www.corner.co.il Books Corner – הוצאת ספרים מקוונת