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.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - Operator Overloading Outline 8.1 Introduction 8.2 Fundamentals of Operator Overloading 8.3.
Operator Overloading Fundamentals
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
© 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 18 - Operator Overloading Outline 18.1Introduction 18.2Fundamentals of Operator Overloading 18.3Restrictions.
8.1 Introduction Use operators with objects (operator overloading) –Clearer than function calls for certain classes –Operator sensitive to context Examples.
2 Objectives You should be able to describe: Operator Functions Two Useful Alternatives – operator() and operator[] Data Type Conversions Class Inheritance.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator Overloading 1. Introduction Let’s define a class for Complex numbers: class Complex { private: double real, image; public: Complex () : real(0.0),
Operator overloading Object Oriented Programming.
Operator Overloading in C++
Review of C++ Programming Part II Sheng-Fang Huang.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - Operator Overloading Outline 8.1Introduction 8.2Fundamentals of Operator Overloading 8.3Restrictions.
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
Object Oriented Programming in C++ Chapter5 Operator Overloading.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.
Operator Overloads Part2. Issue Provide member +(int) operator Rational + int OK int + Rational Error.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
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 Understand Operator Overloading and how it is implemented in C++ ? | Website for students | VTU NOTES.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
CS Object Oriented Programming Using C++
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
1 Chapter 8 - Operator Overloading Outline 8.1Introduction 8.2Fundamentals of Operator Overloading 8.3Restrictions on Operator Overloading 8.4Operator.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 8 - Operator Overloading Outline 8.1Introduction 8.2Fundamentals of Operator Overloading.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - Operator Overloading Outline 8.1Introduction 8.2Fundamentals of Operator Overloading 8.3Restrictions.
1 CSC241: Object Oriented Programming Lecture No 08.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
 2008 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading Ritika Sharma.
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Operator Overloading; String and Array Objects
Polymorphism in C++ Operator Overloading
Object-Oriented Programming (OOP) Lecture No. 21
Operator Overloading BCA Sem III K.I.R.A.S.
Operator Overloading; String and Array Objects
Operator Overloading.
Operator Overloading; String and Array Objects
Operator overloading Dr. Bhargavi Goswami
Chapter 8 - Operator Overloading
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
Operator Overloading; String and Array Objects
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++
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Operator Overloading; String and Array Objects
Chapter 18 - Operator Overloading
Operator Overloading; String and Array Objects
Presentation transcript:

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 data type. Notation :- All overloaded operators have the following declaration operator (Argument-List) Eg.) If we want to overload the ‘+’ operator for a class named Test then the declaration will look like :- Test operator+ (Test& op1, Test& op2);

Elements of the Declaration Let us focus on each element of the declaration –Return-Type :- Must be same as the nature of return-type of the operator being overloaded. Eg.) If we are overloading the >= operator which is a boolean-logic operator then the return type will be bool On the other hand if we are overloading the ‘+’ operator then the return type will be same as the operand type since addition is closed under the data- type it is being used for

Elements of the Declaration (cont.) –Arguments-List The basic fundamental to building the argument-list for an overloaded operator is to note the follwing –Whether the operator is unary or, binary –The context of use that it is being loaded for a.k.a the nature of the operands that it is being overloaded for Eg.) If we want to add an integer to an object of class Test and the operation will always be written as + then the argument-List will be (int& op1, Test& op2) NOTE :- The overloaded operator behaves like a function with its operands as the arguments.

Restrictions on Operator Overloading Cannot change –How operators act on built-in data types I.e., cannot change integer addition –Precedence of operator (order of evaluation) Use parentheses to force order-of-operations –Associativity (left-to-right or right-to-left) –Number of operands & is unitary, only acts on one operand Cannot create new operators Operators must be overloaded explicitly –Overloading + does not overload +=

What can be Overloaded

Converting between Types Casting –Traditionally, cast integers to floats, etc. –May need to convert between user-defined types Cast operator (conversion operator) –Convert from One class to another Class to built-in type ( int, char, etc.) –Must be non- static member function Cannot be friend –Do not specify return type Implicitly returns type to which you are converting

Converting between Types Example –Prototype A::operator char *() const; Casts class A to a temporary char * (char *)s calls s.operator char*() –Also A::operator int() const; A::operator OtherClass() const;

Converting between Types Casting can prevent need for overloading –Suppose class String can be cast to char * –cout << s; // s is a String Compiler implicitly converts s to char * Do not have to overload << –Compiler can only do 1 cast

Conversion Constructor Conversion constructor –Single-argument constructor –Turns objects of other types into class objects String s1(“hi”); Creates a String from a char *

1 // Fig. 8.8: string1.cpp 2 // Member function definitions for class String. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setw; #include // C++ standard "new" operator #include // strcpy and strcat prototypes 15 #include // exit prototype #include "string1.h" // String class definition // conversion constructor converts char * to String 20 String::String( const char *s ) 21 : length( strlen( s ) ) 22 { 23 cout << "Conversion constructor: " << s << '\n'; 24 setString( s ); // call utility function } // end String conversion constructor

class Test { private : int X,Y; float Z; public : 180 // overloaded output operator 181 ostream &operator<<( ostream &output, const String &s ) 182 { 183 output<< s.getX()<<‘\n’; output<< s.getY() <<‘\n’; output<< s.getZ() <<‘\n’; return output; // enables cascading 186 } // end function operator<< // overloaded input operator 190 istream &operator>>( istream &input, String &s ) 191 { int a; float b; 192 input>>a; s.setX(a); input>>a; s. setY(a); input >>b; s. setZ(b); 193 return input; // enables cascading 199 } // end function operator>> };