Polymorphism in C++ Operator Overloading 26-06-2018.

Slides:



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

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. 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 18 - Operator Overloading Outline 18.1Introduction 18.2Fundamentals of Operator Overloading 18.3Restrictions.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
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++
Review of C++ Programming Part II Sheng-Fang Huang.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
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.
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.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
CS Object Oriented Programming Using C++
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
Operator Overloading Moshe Fresko Bar-Ilan University Object Oriented Programing
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 CSC241: Object Oriented Programming Lecture No 08.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Review slides Mar 15, 2005.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading CS 3370 – C++ Chapter 14.
Yan Shi CS/SE 2630 Lecture Notes
Overloading C++ supports the concept of overloading Two main types
Operator Overloading Ritika Sharma.
Chapter 1.2 Introduction to C++ Programming
IS Program Design and Software Tools Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Operator Overloading; String and Array Objects
Operator Overloading; Class string
Visit for more Learning Resources
Basic Elements of C++.
Chapter 15: Overloading and Templates
Operator Overloading BCA Sem III K.I.R.A.S.
Basic Elements of C++ Chapter 2.
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Introduction to C++ Programming
Operator Overloading; String and Array Objects
Operator Overloading.
Operator Overloading; String and Array Objects
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
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++
Engineering Problem Solving with C++ An Object Based Approach
Arrays Arrays A few types Structures of related data items
Operator Overloading I
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Operator Overloading; String and Array Objects
C++ Programming Basics
Chapter 18 - Operator Overloading
Operator Overloading; String and Array Objects
Presentation transcript:

Polymorphism in C++ Operator Overloading 26-06-2018

Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. Operator Overloading is useful for redefining built-in operations for user defined types. Operator Overloading should be used to perform the same function or similar function on class objects as the built-in behavior. Overloading an operator does not change: the operator precedence, the associativity of the operator, the arity(type) of the operator, or the meaning of how the operator works on objects of built- in types. 26-06-2018

Operator Overloading int a = b + c; Each individual operator must be overloaded for use with user defined types. Overloading the assignment operator and the subtraction operator does not overload the -= operator. Operator Overloading enables to apply standard operators (such as +,-,*,<, and so on) to objects of the programmer defined type. It helps to enhance simplicity in program structure. Function name operator+ int a = b + c; int data type Argument to function R-Value , Called Party int data type Calling Party L-Value Returns an int Stored in a 26-06-2018

Operator Overloading What? an operator that has multiple meanings varies depending on use Why? Ease of use is a principle of OO How? by defining as an operator function functions that can extend meaning of built-in operators (cannot define your own new operators) keyword operator is in definition, followed by the operator to be overloaded Used method syntax or operator syntax s1.operator+(s2) vs. s1 + s2 26-06-2018

Why Operation Overloading makes statements more intuitive and readable. for example: Date d1(12,3,1989); Date d2; d2.add_days(d1,45); // can be written with the + operator as d2=d1+45; Extension of language to include user-defined types I.e., classes Make operators sensitive to context Generalization of function overloading 26-06-2018

Restrictions on Overloading The order of precedence cannot be changed for overloaded operators. Default arguments may not be used with overloaded operators New operators cannot be created Overloading must be explicit, i.e. overloading + does not imply += is overloaded 26-06-2018

26-06-2018

Cont.. 26-06-2018

Operator Functions Operator functions may be defined as either member functions or as non-member functions. Non-member functions are usually made friends for performance reasons. Member functions usually use the this pointer implicitly. Syntax: returnType operator symbol(parameters);    any type keyword operator symbol Return type may be whatever the operator returns Including a reference to the object of the operand Operator symbol may be any overloadable operator from the list. 26-06-2018

A basic difference between them is: Operator functions must be either member functions (non static) or friend functions. A basic difference between them is: friend function will have only one argument for unary operators and two for binary operator. Member function has no argument for unary operators and only one for binary. This is because object used to invoke the member function is passed implicitly and therefore is available for the function. In case of friend function argument may be passed either by value or by reference. Vector operator + (Vector); // vector addition Complex operator – (Complex); // Complex subtraction friend operator – (Complex, Complex); // Complex subtraction friend operator ++(int); // unary addition Void operator ++(); // unary addition 26-06-2018

Cont.. The operator overloading functions for overloading (), [], -> or the assignment operators must be declared as a class member. All other operators may be declared as non-member functions. Operator overload function is a function just like any other Can be called like any other – e.g., a.operator+(b) C++ provides the following short-hand a+b If operator overload function is declared global then operator+(a, b) also reduces to the following short-hand 26-06-2018

Cont.. To use any operators on a class object, … The operator must be overloaded for that class. Three Exceptions: {overloading not required} Assignment operator (=) Memberwise assignment between objects Dangerous for classes with pointer members!! Address operator (&) Returns address of the object in memory. Comma operator (,) Evaluates expression to its left then the expression to its right. Returns the value of the expression to its right. 26-06-2018

Unary operator overloading Unary operators are the operators which takes only one operand e.g. ++ , -- . If we want to increment the values of data members, it can be done by overloading operator ++. #include<iostream> using namespace std; class test { private : int x; public: test(int a) x=a; } void operator ++ (void) x++; void show() cout<<”\n the value of data member is “<<x; }; int main() { test t(10); t.show(); ++t; t.show(; return 0; } 26-06-2018

Prefix and Postfix The operator ++ in the above program cannot be used as a postfix operator. To make it for postfix we need to use as: void operator ++(int); The integer above is a dummy argument which is used on to distinguish between the prefix and postfix operators. Now ++t is as operator ++(void) and t++ as operator ++(int) 26-06-2018

Overloading of + operator using member function: Binary Operator The binary operators are arithmetic operator +,-,*,/ and % ,Conditional operators > ,<,<=,>=,== and != and assignment operators like +=,-=,*=,/= etc. Overloading of + operator using member function: comp operator +(comp c2) { comp c3; c3.r=r+c2.r; c3.i=i+c2.i; return c3; } }; class comp { int r,i; public: void st() cout<< "Enter real & imaginary parts"<<endl; cin>>r>>i; } void ts() cout<<"Real = "<<r<<endl; cout<<"Imaginary = "<<i<<endl; int main() { comp b1,b2,b3; b1.st(); b2.st(); b3=b1+b2; b3.ts();} 26-06-2018

Overloading of + operator using Friend function: complex operator+(complex t1,complex t2) { complex temp; temp.real=t1.real+t2.real; temp.imag=t1.imag+t2.imag; return(temp); } int main() complex t1,t2; t1.set(); t2.set(); t1=t1+t2; t1.display(); return(0); #include<iostream> using namespace std; class complex { int real,imag; public: void set() cout<<"enter real&imag"; cin>>real>>imag; } friend complex operator+(complex, complex); void display() cout<<"the sum is"<<real<<"+i"<<imag; }; 26-06-2018

Unary Operators The unary operators operate on the object for which they were called and normally, this operator appears on the left side of the object, as in !obj, -obj, and ++obj but sometime they can be used as postfix as well like obj++ or obj--. Can overload as Non-static member function with no arguments. As a global function with one argument. Argument must be class object or reference to class object. Why non-static? static functions only access static data Not what is needed for operator functions 26-06-2018

Example: Integer Class #include <iostream> using namespace std; // Non-member functions class Integer { long i; Integer* This() return this; } public: Integer(long ll = 0) : i(ll) {} // No side effects takes const& argument: friend const Integer& operator+(const Integer& a); friend const Integer operator-(const Integer& a); friend const Integer operator~(const Integer& a); friend Integer* operator&(Integer& a); friend int operator!(const Integer& a); // Side effects have non-const& argument: friend const Integer& operator++(Integer& a); // Prefix friend const Integer operator++(Integer& a, int); // Postfix friend const Integer& operator--(Integer& a); // Prefix friend const Integer operator--(Integer& a, int); // Postfix }; 26-06-2018

// Prefix; return incremented value const Integer& operator++(Integer& a) { cout << "++Integer\n"; a.i++; return a; } // Postfix; return the value before increment: const Integer operator++(Integer& a, int) cout << "Integer++\n"; Integer before(a.i); return before; // Prefix; return decremented value const Integer& operator--(Integer& a) cout << "--Integer\n"; a.i--; // Postfix; return the value before decrement: const Integer operator--(Integer& a, int) cout << "Integer--\n"; // Global operators: const Integer& operator+(const Integer& a) { cout << "+Integer\n"; return a; // Unary + has no effect } const Integer operator-(const Integer& a) cout << "-Integer\n"; return Integer(-a.i); const Integer operator~(const Integer& a) cout << "~Integer\n"; return Integer(~a.i); Integer* operator&(Integer& a) cout << "&Integer\n"; return a.This(); // &a is recursive! int operator!(const Integer& a) cout << "!Integer\n"; return !a.i; 26-06-2018

// Show that the overloaded operators work: void f(Integer a) { +a; Integer* ip = &a; !a; ++a; a++; --a; a--; } const Byte operator-() const { cout << "-Byte\n"; return Byte(-b); } const Byte operator~() const cout << "~Byte\n"; return Byte(~b); Byte operator!() const cout << "!Byte\n"; return Byte(!b); Byte* operator&() cout << "&Byte\n"; return this; // Member functions (implicit "this"): class Byte { unsigned char b; public: Byte(unsigned char bb = 0) : b(bb) {} // No side effects: const member function: const Byte& operator+() const cout << "+Byte\n"; return *this; } 26-06-2018

void g(Byte b) { +b; -b; ~b; Byte* bp = &b; !b; ++b; b++; --b; b--; } // Side effects: non-const member function: const Byte& operator++() // Prefix { cout << "++Byte\n"; b++; return *this; } const Byte operator++(int) // Postfix cout << "Byte++\n"; Byte before(b); return before; const Byte& operator--() // Prefix cout << "--Byte\n"; --b; const Byte operator--(int) // Postfix cout << "Byte--\n"; }; int main() { Integer a; f(a); Byte b; g(b); } 26-06-2018

Overloading [],(),-> operators [] subscripting () function calling -> class member access Restriction: They must be non-static member functions They can not be friends 26-06-2018

Cont.. // example for [ ] operator #include <iostream.h> Class atype { int a[3]; public: atype(int I,intj,int k) { a[0]=I; a[1]=j; a[2]=k; } int operator[ ] (int i) { return a[i];} // provide array subscripting }; int main() atype ob(1,2,3); Cout << ob[1]; // displays 2 return 0; 26-06-2018

Cont.. //Overloading -> operator Usage : object->element; The operator->() function must return a pointer to an object of the class that operator->() operates upon. Example: #inclide < iostream.h> class myclass { public: int i; myclass *operator->() { return this; } }; int main() { myclass ob; ob->i =10; // same as ob.i cout<< ob.i << “ “ << ob->i; return 0; } 26-06-2018

Example Example program << and >> operators Already overloaded to process each built-in type (pointers and strings). Can also process a user-defined class. Overload using global, friend functions Example program Class PhoneNumber Holds a telephone number Prints out formatted number automatically. (123) 456-7890 26-06-2018

Cont.. // Overloading the stream-insertion and stream-extraction operators. #include <iostream> using std::cout; using std::cin; using std::endl; using std::ostream; using std::istream; #include <iomanip> using std::setw; // PhoneNumber class definition class PhoneNumber { friend ostream &operator<<( ostream&, const PhoneNumber & ); friend istream &operator>>( istream&, PhoneNumber & ); private: char areaCode[ 4 ]; // 3-digit area code and null char exchange[ 4 ]; // 3-digit exchange and null char line[ 5 ]; // 4-digit line and null }; // end class PhoneNumber Notice function prototypes for overloaded operators >> and << They must be non-member friend functions, since the object of class Phonenumber appears on the right of the operator. cin >> object cout<< object 26-06-2018

Cont.. // overloaded stream-insertion operator; cannot be // a member function if we would like to invoke it with // cout << somePhoneNumber; ostream &operator<<( ostream &output, const PhoneNumber &num ) { output << "(" << num.areaCode << ") " << num.exchange << "-" << num.line; return output; // enables cout << a << b << c; } // end function operator<< // overloaded stream-extraction operator; cannot be a member function if we would like to invoke it with // cin >> somePhoneNumber; istream &operator>>( istream &input, PhoneNumber &num ) input.ignore(); // skip ( input >> setw( 4 ) >> num.areaCode; // input area code input.ignore( 2 ); // skip ) and space input >> setw( 4 ) >> num.exchange; // input exchange input.ignore(); // skip dash (-) input >> setw( 5 ) >> num.line; // input line return input; // enables cin >> a >> b >> c; } // end function operator>> The expression: cout << phone; is interpreted as the function call: operator<<(cout, phone); output is an alias for cout. This allows objects to be cascaded. cout << phone1 << phone2; first calls operator<<(cout, phone1), and returns cout. Next, cout << phone2 executes. ignore() skips specified number of characters from input (1 by default). Stream manipulator setw restricts number of characters read. setw(4) allows 3 characters to be read, leaving room for the null character. 26-06-2018

Enter phone number in the form (123) 456-7890: (800) 555-1212 int main() { PhoneNumber phone; // create object phone cout << "Enter phone number in the form (123) 456-7890:\n"; // cin >> phone invokes operator>> by implicitly issuing // the non-member function call operator>>( cin, phone ) cin >> phone; cout << "The phone number entered was: " ; // cout << phone invokes operator<< by implicitly issuing // the non-member function call operator<<( cout, phone ) cout << phone << endl; return 0; } // end main Enter phone number in the form (123) 456-7890: (800) 555-1212 The phone number entered was: (800) 555-1212 26-06-2018