Operator Overloading Fundamentals

Slides:



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

I NTRODUCING O PERATOR O VERLOADING Chapter 6 Department of CSE, BUET 1.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Chapter 13: Overloading.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
OPERATOR OVERLOADING. Closely related to function overloading is - operator overloading. In C++ you can overload most operators so that they perform special.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.
Chapter 14 More About Classes. Chapter 13 slide 2 Topics 13.1 Instance and Static Members 13.2 Friends of Classes 13.3 Memberwise Assignment 13.4 Copy.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
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.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
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.
CS Object Oriented Programming Using C++
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
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.
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 CSC241: Object Oriented Programming Lecture No 08.
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Operator Overloading.
Chapter 18 - C++ Operator Overloading
CSE1002 – Problem Solving with Object Oriented Programming
Chapter 13: Overloading and Templates
Chapter 14: More About Classes.
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Operator Overloading; String and Array Objects
Overloading Operator MySting Example
Object Oriented Programming COP3330 / CGS5409
Constructor & Destructor
Chapter 15: Overloading and Templates
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Chapter 14: More About Classes.
Operator Overloading
Operator Overloading; String and Array Objects
Andy Wang Object Oriented Programming in C++ COP 3330
Advanced Program Design with C++
Operator Overloading; String and Array Objects
Operator overloading Dr. Bhargavi Goswami
Operator Overloading.
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
COP 3330 Object-oriented Programming in C++
Operator Overloading; String and Array Objects
Andy Wang Object Oriented Programming in C++ COP 3330
Presentation transcript:

Operator Overloading Fundamentals Methods (class members & friend functions) Binary operators Stream operators Unary operators Pre & post increment & decrement

Fundamentals Operator overloading is a way to use operators for different data types For example, “+” is used to add int, float, double, etc. Function definition is written the same as any other function Except function name becomes “operator” followed by the symbol that you are overloading operator+

Built-in Operators A few operators are overloaded automatically by the compiler Assignment operator (=) Memberwise assignment of class data members Address operator (&) Returns the address of the object in memory Both can also be explicitly overloaded

Restrictions For a list of operators that can be overloaded See D & D, Chapter 19.3 Five operators cannot be overloaded ., .*, ::, ?:, sizeof These properties cannot be changed Precedence: * has higher precedence than + Associativity: (a + b) + c = a + (b + c) Arity: number of operands that the operator takes (unary & binary operators)

Restrictions Only existing operators can be overloaded Overloading an assignment operator and an addition operator will not automatically overload the += operator F1 = F2 + F3; F1 += F2; Misuse Overloading the “+” operator to subtract

Default Memberwise Copy Assignment operator (=) can be used to assign one object to another By default, performed by memberwise copy Each data member of one object is copied to another object’s data members Can cause problems with dynamically allocated data members (such as linked list) int main(){ Fraction f1, f2(3,4); f1 = f2; f1.print(); // 3/4 f2.print(); // 3/4 return 0; }

Default Memberwise Copy If you were to write it yourself, it would look like this See complete program at assign.cpp const Fraction &operator=(const Fraction &f){ num=f.num; den=f.den; return *this; /*a pointer to the current object*/ }

Method #1 Class Members: overloading an operator by adding another method (member function) to the class itself Must use this method for overloading (), [], -> or any of the assignment operators Can use this method when the leftmost operand is a class object This works fine with f1 = f2 + 2; which converts “2” to a fraction (via a conversion constructor) and adds it to f2 But will not work with f1 = 2 + f2; since “2” is not of class fraction

Method #1 (see method1.cpp) c.operator=(a.operator+(b)) class Fraction { int num, den; public: Fraction(int n, int d) {num=n;den=d;} Fraction operator+ (const Fraction &f){ Fraction temp; temp.num=num*f.den+f.num*den; temp.den=den*f.den; return temp;} void print() const{ cout<<num<<'/'<<den<<endl;} }; Note: c=a+b is interpreted as (see method1.cpp) c.operator=(a.operator+(b)) void main(){ Fraction a(1,3); Fraction b(2,3); Fraction c; c = a + b; c.print(); // 1/1 }

Method #2 Friend Functions: overloading an operator as a friend function (non-member function) of the class Can use this method when the leftmost or rightmost operand is a class object This will fix our problem when we have a different class object on the left side of an operator Must use this method when the leftmost operand is NOT a class object (for example, cout<<fraction2)

Method #2 (See method2.txt) c.operator=(operator+(a, b)) void main(){ Fraction a(1,3); Fraction b(2,3); Fraction c; c = a + b; c.print(); // 1/1 } class Fraction { int num, den; public: Fraction(){num=0;den=1;} Fraction(int n, int d) {... num=n;den=d;} friend Fraction operator+ (const Fraction &a, const Fraction &b){ Fraction temp; temp.num=a.num*b.den+b.num*a.den; temp.den=a.den*b.den; return temp;} void print() const{ cout<<num<<'/'<<den<<endl;} }; Note: c=a+b is interpreted as (See method2.txt) c.operator=(operator+(a, b))

Why Does This Program Run? class Fraction { int num, den; public: Fraction(){num=0;den=1;} Fraction(int n) {num=n;den=1;} friend Fraction operator+ (const Fraction &a, const Fraction &b){ Fraction temp; temp.num=a.num*b.den+b.num*a.den; temp.den=a.den*b.den; return temp;} void print() const{ cout<<num<<'/'<<den<<endl;} }; void main(){ Fraction a, b; a = 1 + a; b = 2.5 + b; a.print(); //1/1 b.print(); //2/1 } (See run.cpp)

Because… C++ will try and convert types to perform operations requested Asks: Can I convert an “int” to a Fraction? Yes, we have a constructor that takes an integer and returns the equivalent Fraction System invokes constructor to build Fraction from int What about “Fraction = double + Fraction? Affirmative. Can convert double to int (truncate) Can then convert int to Fraction (constructor)

Conversion Constructor A constructor that transforms objects of one type into objects of another type Example constructor that converts int to Fraction: Fraction(int n){num=n;den=1;}

Overloading Stream Operators cin object An instance (variable) of the istream class operator>> So cin>>a>>b; becomes operator>>(cin,a); operator>>(cin,b); cout object An instance (variable) of the ostream class operator<< So cout<<a<<b; becomes operator<<(cout,a); operator<<(cout,b);

Stream Operators class Fraction{ . . . friend istream & operator>>(istream &in, Fraction &f){ char ch; in>>f.num>>ch>>f.den; return in;} friend ostream & operator<<(ostream &out, const Fraction & f){ out<<f.num<<'/'<<f.den; return out;} }; int main(){ Fraction a,b; cin>>a>>b; cout<<a<<" "<<b<<endl; return 0; } (See stream.cpp)

Overloading Unary Operators Can be overloaded as a class member or as a friend function For example, the logical not operator (!) Will change true to false Or change false to true Returns either true (non-zero) or false (zero) Use bool (boolean) data type

Overloading Unary Operators As a class member function bool operator!()const{ if(0!=num){return false;} return true; } As a friend function friend bool operator!(const Fraction &f){ if(0!=f.num){return false;} } (See unary.cpp)

Overloading ++ & -- Preincrement Original code: ++f1; Compiler will generate: f1.operator++(); Function prototype: Fraction &operator++(); Returns a reference to itself

Overloading ++ & -- Postincrement Original code: f1++; Compiler will generate: f1.operator++(0); This is a “dummy value” to distinguish it from preincrement Function prototype: Fraction operator++(int); Value return, not a reference return

Unary Operators class Fraction{ . . . Fraction &operator++(){//preincrement num+=den; return *this;} Fraction operator++(int){//postincrement Fraction temp = *this; return temp;} }; void main(){ Fraction a,b,c; b = ++a; //b.operator=(a.operator++()) c = a++; //c.operator=(a.operator++(0)) cout<<a<<" "<<b<<" "<<c<<endl; } (see prepost.cpp)