ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

ECE 264 Object-Oriented Software Development
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 21: Strings (cont.)
Operator Overloading Fundamentals
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.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Chapter 13: Overloading.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 15: Class diagrams; class relationships.
Chapter 15: Operator Overloading
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
Operator Overloading in C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 6: Continuing with output formatting.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Michael Geiger Spring 2009 Lecture 2: Basic C++ Programs.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 2: Software Design Cycle.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Exam 3 Preview.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 31: Operator overloading examples, inheritance intro.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 11: Class diagrams; class relationships.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
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,
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
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.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 18: More on inheritance and Polymorphism.
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.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
1 CSC241: Object Oriented Programming Lecture No 08.
ECE 264 Object-Oriented Software Development
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 11: Examples—creating a basic class.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 16: Destructors, Copy Constructors and Exam 2 Review.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes.
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 17: Operator overloading and inheritance intro.
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
Operator Overloading What is operator overloading? Most predefined operators (arithmetic, logic, etc.) in C++ can be overloaded when applied to objects.
Operator Overloading.
Chapter 13: Overloading and Templates
ECE 264 Object-Oriented Software Development
Chapter 15: Overloading and Templates
Andy Wang Object Oriented Programming in C++ COP 3330
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Andy Wang Object Oriented Programming in C++ COP 3330
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
(4 – 2) Introduction to Classes in C++
Presentation transcript:

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading

Lecture outline Announcements / reminders  Project designs due tonight or submit to your group folder  Exam 2 grades on 11/19  Project design due on 11/19  Lab 8 to be posted by Monday (11/12) night Today  Review copy constructors  Operator overloading 12/24/2015 ECE 264: Lecture 19 2

Copy constructors Used to initialize a newly declared variable from an existing variable  Not called for assignments  Example: Point p1(2,3), p3; Point p2 = p1;// calls copy constructor p3 = p2;// uses assignment Default behavior: shallow copy  Directly copy data members  Doesn’t work for arrays and pointers  Perform deep copy—allocate enough space for data, then copy the data (not the pointer) ECE 264: Lecture /24/2015

Motivating operator overloading Recall earlier example Point p1(2,3), p3; Point p2 = p1;// calls copy constructor p3 = p2;// uses assignment We handled first case with copy constructors What about second case?  Uses assignment operator  How is that operator defined? 12/24/2015 ECE 264: Lecture 19 4

Overloading Overloading: giving a function more than one meaning  We’ve seen examples of overloading before Constructors: default vs. parameterized vs. copy  All have same name, but different arguments  We’ve even seen overloaded operators String concatenation: + +=  Works with strings, char*, single characters, etc. Input/output operators: >  Work with any built-in type Can define own operators that allow use of our classes in same manner as built-in type 12/24/2015 ECE 264: Lecture 19 5

Overloading Operators Definitions for overloaded operators (usually) included in class  Look just like functions  May be binary or unary  Example double operator –(const Point& p2);  Syntax: Keyword operator is used, followed by the actual operator “Argument” is (usually) an object reference of the same type const declarations are optional but often preferred  Only predefined operators may be overloaded. Exceptions:. ::.* ?: sizeof ECE 264: Lecture /24/2015

Example: Overloaded Point operator //Distance Formula double Point::operator –(const Point& rhs) { double t1, t2, d; t1 = rhs.xCoord – xCoord; //(x2-x1) t2 = rhs.yCoord – yCoord; //(y2-y1) d = std::sqrt( std::pow(t1,2) + std::pow(t2,2) ); return d; } 12/24/2015 ECE 264: Lecture 5 7 xCoord and yCoord are provided by the calling object. To access the corresponding data from the object on the right hand side of the operator, use rhs.xCoord and rhs.yCoord

Forms of overloaded operators Member functions  Left hand side of binary operator (x, if operation is x + y) invokes operator Operator requires one argument (y)  Object used with unary operator (e.g., -x) invokes operator  Type of x must match the class Friend functions  Typically used when left hand side of binary operator does not match class For example, cout << x  cout (type ostream ) is on LHS Want operator to have access to data members of class 12/24/2015 ECE 264: Lecture 10 8

Friend Functions binary operators  friend function requires two arguments unary operator  friend function requires one argument Disadvantage  A friend function is NOT a member function  Friend functions violate a strict interpretation of object oriented principals (implementation is hidden)  Recommended for operator overloading only Particularly useful with stream operators > 12/24/2015 ECE 264: Lecture 10 9

Example: In class definition (.h file): friend complex operator +(complex c1, complex c2); In class implementatio (.cpp file) n: complex operator +(complex c1, complex c2) { complex temp; temp.real = c1.real + c2.real; temp.imag = c1.imag + c2.imag; return temp; } In client program: complex cA, cB, cC; cC = cA+cB; cC = cB; //this is ok, when + is a friend function 12/24/2015 ECE 264: Lecture 10 10

Final notes Next time  Operator overloading examples  Inheritance Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: Deitel & Deitel, C++ How to Program, 8 th ed. Etter & Ingber, Engineering Problem Solving with C++, 2 nd ed. 12/24/2015 ECE 264: Lecture 19 11