Operator Overloading BCA Sem III K.I.R.A.S.

Slides:



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

CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.
Operator Overloading Fundamentals
Operator Overloading. Basics Define operations on user-defined data types –Perform same operation as it is supposed to but, with operands of the user-defined.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
Chapter 15: Operator Overloading
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator overloading Object Oriented Programming.
Operator Overloading in C++
1 CSC241: Object Oriented Programming Lecture No 07.
Review of C++ Programming Part II Sheng-Fang Huang.
Operator Overloading and Type Conversions
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
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.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Object-Oriented Programming (OOP) Lecture No. 16
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading Ritika Sharma.
Friend functions, operator overloading
Chapter 13: Overloading and Templates
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Operator Overloading; String and Array Objects
Java Primer 1: Types, Classes and Operators
Visit for more Learning Resources
Object Oriented Programming COP3330 / CGS5409
Operator Overloading.
Developed By : Ms. K. S. Kotecha
Polymorphism in C++ Operator Overloading
Object-Oriented Programming (OOP) Lecture No. 21
Constructor & Destructor
Object-Oriented Programming (OOP) Lecture No. 16
Lecture Set 4 Data Types and Variables
Operators and Expressions
Operators Lecture 10 Fri, Feb 8, 2008.
Introduction to C++ Programming
Operator Overloading; String and Array Objects
Operator Overloading.
Operator Overloading; String and Array Objects
Chapter-3 Operators.
Operator overloading Dr. Bhargavi Goswami
Operator Overloading.
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
Lecture 7.
Operator overloading Friend Function This Operator Inline Function
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Operator Overloading BCA Sem III K.I.R.A.S

What is operator overloading? Operator overloading is a form of polymorphism i.e performing many actions with a single operator The operator keyword declares a function specifying what operator-symbol means when applied to instances of a class. This gives the operator more than one meaning, or "overloads" it. The compiler distinguishes between the different meanings of an operator by examining the types of its operands One of the nice features of C++ is that you can give special meanings to operators, when they are used with user-defined classes. This is called operator overloading. You can implement C++ operator overloads by providing special member-functions on your classes that follow a particular naming convention. C++ enables u to build operators that implement unary and binary operations on objects of classes. THIS FEATURE IS CALLED OPERATOR OVERLOADING ,and with this u can add member functions to the class to implement the overloaded operators

What is operator overloading? An operator is overloaded by writing a non-static member function definition or a global function definition except that the function name becomes the keyword operator followed by the symbol for the operation being overloaded. Operator overloading means it is simply another way for you to make a function call. The difference is that the arguments for this function don’t appear inside parentheses, but instead they surround or are next to characters you’ve always thought of as immutable operators. operator overloading is a specific case of polymorphism in which some or all of operators like +, =, or == have different implementations depending on the types of their arguments.

What is operator overloading? There are two differences between the use of an operator and an ordinary function call. The syntax is different; an operator is often “called” by placing it between or sometimes after the arguments. The second difference is that the compiler determines which “function” to call. For instance, if you are using the operator + with floating-point arguments, the compiler “calls” the function to perform floating-point addition (this “call” is typically the act of inserting in-line code, or a floating-point-processor instruction). If you use operator + with a floating-point number and an integer, the compiler “calls” a special function to turn the int into a float, and then “calls” the floating-point addition code. But in C++, it’s possible to define new operators that work with classes. This definition is just like an ordinary function definition except that the name of the function consists of the keyword operator followed by the operator. That’s the only difference, and it becomes a function like any other function, which the compiler calls when it sees the appropriate pattern.

What is operator overloading C++ tries to make the user-defined data types behave in much the same way as the built-in data types. In built-in data types we have: c=a+b //a,b and c are of type ‘int’. We can also have in C++: object1=object2+object3; C++ has the ability to provide the operators with a special meaning for a data type. This mechanism is known as operator overloading. The concept of operator overloading can also be applied to data conversions. C++ offers automatic conversion of primitive data types. For ex: x =a + b, the compiler implicitly converts the result to floating point representation and then assigns it to the float variable “ x” Use of operator overloading : Extending the capability of operators to operate on user defined data Data conversion

Operator Overloading Syntax Syntax is: Examples: operator+ operator- operator* operator/ Returntype operator symbol(arg-list) --- operator is a keyword --- symbol is one of C++ operator symbols (+, -, =, etc..)

Operator Overloading Overview Use operators with objects (operator overloading) Clearer than function calls for certain classes Examples of operator overloading: firststring + secondstring might concatenate two string objects myDate++ might increment a Date object a * b might multiply two Number objects

Operator Overloading Overview Many C++ operator are already overloaded for primitive types. Examples: + - * / << >> It is often convenient for our classes to imitate the operations available on primitive types (e.g., + or - ). Then we can use the same concise notation for manipulating our objects. Operator functions must be either member functions or friend functions. vector operator+(vector); vector operator-(); friend vector operator+(vector,vector); friend vector operator-(vector); int operator==(vector); friend int operator==(vector,vector); friend void operator-(space &s);  

Why Operator Overloading int i, j, k; // integers float m, n, p; // floats k = i + j; // integer addition and assignment p = m + n; // floating addition and assignment The compiler overloads the + operator for built-in integer and float types by default, producing integer addition with i+j, and floating addition with m+n.

Implementing Operator Overloading Two ways: 1)Implemented as member functions 2)Implemented as non-member or Friend functions (the operator function may need to be declared as a friend if it requires access to private data ) Expression object1@object2 translates into a function call object1.operator@(object2), if this function is defined within class object1 operator@(object1,object2), if this function is defined outside the class object1

Implementing Operator Overloading Defined as a member function class Complex { ... public: Complex operator +(const Complex &op) { double real = _real + op._real, imag = _imag + op._imag; return(Complex(real, imag)); } }; c = a+b; c = a.operator+ (b);

Implementing Operator Overloading Defined as a non-member function class Complex { ... public: double real() { return _real; } //need access functions double imag() { return _imag; } }; c = a+b; c = operator+ (a, b); Complex operator +(Complex &op1, Complex &op2) { double real = op1.real() + op2.real(), imag = op1.imag() + op2.imag(); return(Complex(real, imag)); }

Implementing Operator Overloading Defined as a friend function class Complex { ... public: friend Complex operator +( const Complex &, const Complex & ); }; c = a+b; c = operator+ (a, b); Complex operator +(Complex &op1, Complex &op2) { double real = op1._real + op2._real, imag = op1._imag + op2._imag; return(Complex(real, imag)); }

A Complex Number Class class Complex { private: int real; int imagine; public: Complex (int real = 0, int imagine = 0); int getReal ( ) const; int getImagine ( ) const; void setReal (int n); void setImagine (int d); }; It makes sense to want to perform mathematical operations with Complex objects. Complex C1 (3, 5), C2 (5, 9), C3; C3 = C1 + C2; // addition C2 = C3 * C1; // subtraction C1 = -C2; // negation

Operators Are Really Functions For user-defined types, when you use an operator, you are making a function call. Consider the expression: C2 + C1 This is translated into a function call. The name of the function is “operator+” The call is: C2.operator+(C1);

Commonly Overloaded Operators Unary ++, --, (increment, decrement) ( ), (function call) [ ], (subscript) +, - (sign) Binary +, -, *, /, % (arithmetic) =, +=, -+, *=, /=, %= (assignment) &, |, ^, ^=, &=, |= (bitwise) ==, !=, >, <, >=, <= (relational) ||, && (logical) <<, >> (shift)

Restrictions Not all operators can be overloaded. You can’t make up your own operators. The order of precedence cannot be changed for overloaded operators. Default arguments may not be used with overloaded operators Overloading must be explicit, i.e. overloading + does not imply += is overloaded Badly overloaded operators can confuse a programmer