© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

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.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
© 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
1 Operator Overloading. 2 Syntax The general syntax is: [friend] returntype operator ( ) { ; }
Lecture 4 OOP Course. 4. Operators Using constructors: String int main(){ String s1(“My String”); String s2(s1); String s3; s3=s1; } int main(){ String.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
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 CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
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 and Type Conversions
Object Oriented Programming in C++ Chapter5 Operator Overloading.
1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
Operator Overloading Like most languages, C++ supports a set of operators for its built-in types. Example: int x=2+3; // x=5 However, most concepts for.
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.
Copyright  Hannu Laine C++-programming Part 4: 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.
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. 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.
C# Operator Overloading and Type Conversions C#.NET Software Development Version 1.0.
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
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,
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
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.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Previous lecture Introduction to OOP and C++ Data Abstraction –String example.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
Operator Overloading CS 3370 – C++ Chapter 14.
Yan Shi CS/SE 2630 Lecture Notes
Chapter 14: More About Classes.
Operator overloading Conversions friend inline
Operator overloading Conversions friend inline
Class: Special Topics Copy Constructors Static members Friends this
Jim Fawcett Copyright ©
Operator Overloading CSCE 121 J. Michael Moore
Chapter 14: More About Classes.
תכנות מכוון עצמים ו- C++ יחידה 06 העמסת אופרטורים
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
Operator Overloading I
Review of Function Overloading
Destructors, Copy Constructors & Copy Assignment Operators
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
Destructors, Copy Constructors & Copy Assignment Operators
Jim Fawcett Copyright ©
Presentation transcript:

© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0

© Copyright Eliyahu Brutman Chapter 4 – Copy Constructor, Overloading Operators Version 1.0

© Copyright Eliyahu Brutman Overloading & Operators - 3 String copy-constructor char* String::getString(){return chars;} String::String(const String& other){ length = other.length; chars = new char[length+1]; strcpy(other.chars, chars); } String.cpp

© Copyright Eliyahu Brutman Overloading & Operators - 4 Copy-constructors Single parameter – reference to the copied object class X{ public X(const X& x); } Called by the compiler When an object is initialized using another object of the same class  X x; X x1(x); When an object is passed by value  void foo(X xparam); foo(x); If no copy-constructor defined, compiler adds one Bitwise copy of the copied object Not always fits

© Copyright Eliyahu Brutman Overloading & Operators - 5 Using constructors: String void main(){ String s1(“My String”); String s2(s1); String s3=s1; } What’s the difference? void main(){ String s1(“My String”); String s2(s1); String s3; s3=s1; }

© Copyright Eliyahu Brutman Overloading & Operators - 6 Operators Definition We want User-defined types to behave the same way as built-in types. We want operators to be supported, for the uniform convention The language allows this And the usage: class String { char* m_str; int m_len; public: String(); String(char*); ~String(); int operator>(const String& s); String operator+(const String& s); char* GetString(); void SetString(char* newStr); };

© Copyright Eliyahu Brutman Overloading & Operators - 7 Operators - Usage Who is the caller? Who is the parameter? void main() { String s1= “ Computer ” ; String s2= “ Techniques ” String s3 = s1+s2;// not operator=, but CCtor } Invokes “function” s1.operator+(s2)

© Copyright Eliyahu Brutman Overloading & Operators - 8 Operators - Implementation int String::operator>(const string& s) { return strcmp(m_str, s.m_str); }

© Copyright Eliyahu Brutman Overloading & Operators - 9 Operators - Implementation String String::operator+(const string& s) { char* old_str = m_str; m_str = new char[m_len+s.m_len+1]; m_len+=s.m_len; strcpy(m_str, old_str); strcat(m_str, s.m_str); delete [] old_str; return this; } Within a member function: explicit name for the object on which the function is called

© Copyright Eliyahu Brutman Overloading & Operators - 10 Operators: Good news Can be defined and re-defined Defined by default: & (unary), =,, Rules: At least one operand must be class Cannot define new operators Cannot change precedence/associativity Cannot redefine., ?:, sizeof, ::,.,*

© Copyright Eliyahu Brutman Overloading & Operators - 11 Example: Class Vector class Vector2D{ private: double x, y; public: Vector2D(double x_init=0, double y_init=0): x(x_init), y(y_init){} double getX() const {return x;} double getY() const {return y;} Vector2D add(const Vector2D& other) const {return Vector2D(x+other.x, y+other.y);} } //usage: Vector2D a(3,2), b(1); Vector2D c = a.add(b); cout << “(“ << c.getX() << “,” << c.getY() << ‘)’;

© Copyright Eliyahu Brutman Overloading & Operators - 12 Example: Class Vector class Vector2D{ private: double x, y; public: Vector2D(double x_init=0, double y_init=0): x(x_init), y(y_init){} double getX() const {return x;} double getY() const {return y;} Vector2D operator+(const Vector2D& other) const {return Vector2D(x+other.x, y+other.y);} } //usage: Vector2D a(3,2), b(1); Vector2D c = a+b; cout << “(“ << c.getX() << “,” << c.getY() << ‘)’;

© Copyright Eliyahu Brutman Overloading & Operators - 13 Overloading ++ and -- Pre- and post-increment x = 0; cout << x++; cout <<++x; Pre-increment and pre-decrement – usual unary operators Vector2D operator++(); Post-increment and post-decrement – use dummy parameter Vector2D operator++(int); Rule of thumb: for performance reasons, use the “ pre ” versions

© Copyright Eliyahu Brutman Overloading & Operators - 14 Friend access modifier The following will work: c=a+b; c=a+1; The following will not work: c=1+b; The first argument cannot be built-in! Solution: define the operator not as a member function But what about encapsulation? Access modifier friend : allows to access private fields and methods of the class

© Copyright Eliyahu Brutman Overloading & Operators - 15 Example: Class Vector class Vector2D{ private: double x, y; public: Vector2D(double x_init=0, double y_init=0): x(x_init), y(y_init){} double getX() const {return x;} double getY() const {return y;} friend Vector2D operator+( const Vector2D& left, const Vector2D& right); } Vector2D operator+(const Vector2D& left, const Vector2D& right){ return Vector2D(left.x+right.x, left.y+right.y);} //usage: Vector2D a(3,2), b(1); Vector2D c = a+b; cout << “(“ << c.getX() << “,” << c.getY() << ‘)’;

© Copyright Eliyahu Brutman Overloading & Operators - 16 Class Vector: more operators class Vector2D{ … Vector2D operator-() const{return Vector2D(-x, -y);} double operator[](int index) const {return (index == 0)?x:y;} friend ostream& operator<<( ostream& ostr, const Vector2D& v); } ostream& operator<<(ostream& ostr, const Vector2D& v){ return ostr << “(“ << v.x << “,” << v.y << ‘)’; } //usage: Vector2D a(3,2), b(1); Vector2D c = -a; cout << “(“ << c[0] << “,” << c[1] << ‘)’; cout << c << b << a;

© Copyright Eliyahu Brutman Overloading & Operators - 17 operator= By default: bitwise copy Will this work for Vector2D? Will this work for String? Rule of thumb: either you need all of {destructor, copy- constructor, operator=} or you need none Rules: MyClass& MyClass::operator=(const MyClass& other) Return *this Define as member to ensure target object is assignable

© Copyright Eliyahu Brutman Overloading & Operators - 18 operator= : example class String{ … public: String& operator=(const String& other){ if (this == &other) return *this; if (m_str != NULL) delete[] m_str; m_len = other.m_len; m_str = new char[m_len+1]; strcpy(m_str, other.m_str); return *this; }