Download presentation
Presentation is loading. Please wait.
Published byOwen Chase Modified over 9 years ago
1
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading
2
Lecture outline Announcements / reminders Project designs due tonight E-mail 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
3
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 19 312/24/2015
4
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
5
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
6
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 19 612/24/2015
7
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
8
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
9
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
10
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 = 54.3 + cB; //this is ok, when + is a friend function 12/24/2015 ECE 264: Lecture 10 10
11
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.