Operator Overloading.

Slides:



Advertisements
Similar presentations
1 Data Structures - CSCI 102 CS102 C++ Operator Overloading Prof Tejada.
Advertisements

Introduction to Programming Lecture 31. Operator Overloading.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Operator Overloading. 2 Objectives Discuss operator overloading –definition –use –advantages –limitations Present type conversion operators.
Operator overloading Object Oriented Programming.
Performing Simple Calculations with C# Svetlin Nakov Telerik Corporation
Operator Overloading and Type Conversions
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Operator overloading and type convesions BCAS,Bapatla B.mohini devi.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II.
Operator Overloading. Introduction Computer is calculating machine. It calculates the data provided to it. It performs the various operations on data.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Performing Simple Calculations with C# Telerik Corporation
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Operators and Expressions
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Rational Expressions relational operators logical operators order of precedence.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
Operator Overloading.
C++ : Operator overloading example
Operator Overloading Ritika Sharma.
Chapter 7: Expressions and Assignment Statements
Data types Data types Basic types
Operator Overloading; String and Array Objects
Section 3 Review Mr. Crone.
COMP 170 – Introduction to Object Oriented Programming
More important details More fun Part 3
Operator Overloading.
CSC241: Object Oriented Programming
Chapter 7: Expressions and Assignment Statements
Programming Fundamentals
Operator Overloading BCA Sem III K.I.R.A.S.
SELECTION STATEMENTS (1)
Operators and Expressions
Operators and Expressions
Statements, Comments & Simple Arithmetic
Operator Overloading
Operators Lecture 10 Fri, Feb 8, 2008.
Advanced Programming Lecture 02: Introduction to C# Apps
An Introduction to Java – Part I, language basics
Relational Operators Operator Meaning < Less than > Greater than
Operator Overloading; String and Array Objects
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
Operator Overloading; String and Array Objects
Introduction to Programming
محاور المحاضرة خوارزمية الفقاعات الهوائية (Bubble Sort)
The University of Texas – Pan American
Bools & Ifs.
3 Control Statements:.
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall 2012 This set of slides is revised from lecture.
Chapter 3 – Introduction to C# Programming
Operator Overloading.
class PrintOnetoTen { public static void main(String args[]) {
Introduction to Programming – 4 Operators
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Expressions.
The important features of OOP related to C#:
9-10 Classes: A Deeper Look.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT)
Operator Overloading; String and Array Objects
Names of variables, functions, classes
Just Enough Java 17-May-19.
OPERATORS in C Programming
Operator King Saud University
Introduction to Programming
Standard Version of Starting Out with C++, 4th Edition
9-10 Classes: A Deeper Look.
OPERATORS in C Programming
Presentation transcript:

Operator Overloading

Introduction C# supports operator overloading, it means user defined data type like class and structure can be use much as the same way as built-in type with operators Eg: Class A { ….. } A p,q,r; r=p+q;

Overloadable Operators CATEGORY OPERATORS Binary Arithmetic +,-,*,/,% Unary Arithmetic +,-,++,-- Binary Bitwise &,|,^,<<,>> Unary Bitwise !,~,true,false Logical Operators ==,!=,>=,<=,<,>

Operators that can not be overloaded CATEGORY OPERATORS Conditional Operators &&,|| Compound Assignment +=,-=,*=,/=,%= Other Operators [],(),=,?:,->,new, sizeof,typeof,is, as

Defining Operator Overloading To define additional task to an operator, we must specify what it means in relation to the class This is done with the help of special method called operator method The general form of this method is Public static retval operator op (arglist) { method body; //task defined }

Cont.. They must be defined as public static The retval (return value) type is the type we get when we use this operator, but technically it can be of any type The arglist is the list of arguments passed One argument for unary operator Two arguments for binary operator In the case of unary operators, the argument must be the same type of the enclosing class or structure In the case of binary operators, first argument must be the same as class, and second may be of any type Eg: Public static vector operator + (vector a, vector b) //unary minus Public static vector operator – (vector a) // Comparison Public static bool operator == (vector a, vector b)

Overloading unary operator

public Space(int a, int b, int c) x = a; y = b; z = c; } class Space { int x, y, z; public Space(){} public Space(int a, int b, int c) x = a; y = b; z = c; } public void display() Console.WriteLine("X=" + x + "y=" + y + "z=" + z); public static Space operator - (Space s) Space temp=new Space(); temp.x = -s.x; temp.y = -s.y; temp.z = -s.z; return (temp); class Program { static void Main(string[] args) Space s = new Space(10, -20, 30); Console.WriteLine("B4 overloading "); s.display(); s = -s; Console.WriteLine("After overloading"); Console.Read(); }

Overloading Binary Operator

class complex { double x, y; public complex() { } public complex(double real, double imag) x = real; y = imag; } public static complex operator +(complex c1, complex c2) complex c3 = new complex(); c3.x = c1.x + c2.x; c3.y = c1.y + c2.y; return (c3); public void display() Console.Write(x); Console.Write(" +j" + y); Console.WriteLine(); class Program { static void Main(string[] args) complex a, b, c; a = new complex(2.5, 3.5); b = new complex(1.5, 0.5); Console.Write("a="); a.display(); Console.Write("b="); b.display(); c = a + b; Console.Write("c="); c.display(); Console.Read(); }

Overloading Comparison Operator

C# supports six comparison in three pair == and ! = > and < = < and >= The significance of pairing is : Within each pair, second operator should always give exactly the opposite result to the first That is, whenever first returns true, the second return false C# always requires us to overload the comparison operator in pair, means if we overload = =, than we must overload ! = also, otherwise it is an error

Cont.. There is fundamental difference between overloading comparison operator and arithmetic operator Comparison operator must return bool type value

class test { int a, b, c; public test() { } public test(int x, int y, int z) a = x; b = y; c = z; } public static bool operator ==(test t1, test t2) if (t1.a == t2.a && t1.b == t2.b && t1.c == t2.c) return (true); else return (false); public static bool operator !=(test t1, test t2) return (!(t1 == t2)); class Program { static void Main(string[] args) test t1 = new test(10, 20, 30); test t2 = new test(10, 20, 30); if (t1 == t2) Console.WriteLine("equal"); else Console.WriteLine("Not Equal"); Console.Read(); }