Customizing Operators for Classes at IIT Bombay

iit bombay n.w
1 / 20
Embed
Share

Join Dr. Deepak B. Phatak and Dr. Supratik Chakraborty at IIT Bombay for insights into customizing operators for classes, operator overloading, and assignment overloading. Dive into object-oriented programming topics and practical examples in this engaging lecture session.

  • IIT Bombay
  • Operator Overloading
  • Object-oriented Programming
  • Dr. Deepak B. Phatak
  • Dr. Supratik Chakraborty

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. IIT Bombay Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Operator Overloading Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 1

  2. Quick Recap of Relevant Topics Quick Recap of Relevant Topics IIT Bombay Object-oriented programming with structures and classes Accessing data members and member functions Constructors and destructors Function calls with structures and classes 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

  3. Overview of This Lecture Overview of This Lecture IIT Bombay Customizing operators for classes Operator overloading Assignment overloading 3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

  4. Acknowledgment Acknowledgment IIT Bombay Much of this lecture is motivated by the treatment in An Introduction to Programming Through C++ by Abhiram G. Ranade McGraw Hill Education 2014 Examples taken from this book are indicated in slides by the citation AGRBook Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 4

  5. Motivating Operator Overloading Motivating Operator Overloading IIT Bombay class V3 { private: double x, y, z; public: Constructor, destructor, other member functions V3 sum (const V3 &b) { V3 v; v.x = x + b.x; v.y = y + b.y; v.z = z + b.z; return v; } }; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay Recall Class V3 5

  6. Motivating Operator Overloading Motivating Operator Overloading IIT Bombay int main() { V3 vel, acc, pos; V3 currDispl, currPos; double t, deltaT, totalT; Some code here while (t <= totalT) { currDispl = (vel.scale(t)).sum(acc.scale(0.5*t*t)); currPos = currDispl.sum(pos); t = t + deltaT; } Some code here } Recall Motion Simulator Isn t that too clumsy? Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 6

  7. Motivating Operator Overloading Motivating Operator Overloading IIT Bombay int main() { V3 vel, acc, pos; V3 currDispl, currPos; double t, deltaT, totalT; Some code here while (t <= totalT) { currDispl = (vel * t) + 0.5 * (acc * (t*t)); currPos = currDispl + pos; t = t + deltaT; } Some code here } Can we write this instead? Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 7

  8. Motivating Operator Overloading Motivating Operator Overloading IIT Bombay Normally + and * operators in C++ don t operate on V3 objects as operands Can we overload their meaning to operate on V3 objects? Yes, indeed! C++ provides a way of achieving this!!! Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 8

  9. Understanding Infix Operators in C++ Understanding Infix Operators in C++ IIT Bombay Suppose @ is an infix operator (e.g. +, -, /, %, ) Written between operands, as in X @ Y In C++, the expression X @ Y is equivalent to X . operator@ (Y) Call to member function operator@ of class of X Invoked on receiver object X Parameter passed is object Y C++ keyword Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 9

  10. Defining Custom Operators for Class V3 Defining Custom Operators for Class V3 IIT Bombay class V3 { private: double x, y, z; public: Constructor, destructor, other member functions V3 operator+ (const V3 &b) { return V3(x + b.x, y + b.y, z + b.z); } V3 operator* (const double factor) { return V3(x*factor, y*factor, z*factor); } }; Replaced sum with operator+ Replaced scale with operator* Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 10

  11. Defining Custom Operators for Class V3 Defining Custom Operators for Class V3 IIT Bombay class V3 { private: double x, y, z; public: Constructor, destructor, other member functions V3 operator+ (const V3 &b) const { return V3(x + b.x, y + b.y, z + b.z); } V3 operator* (const double factor) const { return V3(x*factor, y*factor, z*factor); } }; Preferable to use const. Denotes that member function cannot change receiver object Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 11

  12. C++ Program With Overloaded Operators C++ Program With Overloaded Operators IIT Bombay int main() { V3 vel, acc, pos; V3 currDispl, currPos; double t, deltaT, totalT; Some code here while (t <= totalT) { currDispl = (vel * t) + 0.5 * (acc * (t*t)); currPos = currDispl + pos; t = t + deltaT; } Some code here } Invoking member function Legitimate C++ code operator* This appears problematic! Recall: X@Y and X.operator@(Y) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 12

  13. Another Overloading Technique Another Overloading Technique IIT Bombay C++ also allows us to define operator@ as an ordinary (non- member) function, and use @ as an infix operator in expressions V3 operator* (const double factor, const V3 &b) { return (b * factor); } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 13

  14. Another Overloading Technique Another Overloading Technique IIT Bombay C++ also allows us to define operator@ as an ordinary (non- member) function, and use @ as an infix operator in expressions Note the order of typed operands. Allows (factor * b) to be evaluated V3 operator* (const double factor, const V3 &b) { return (b * factor); } Invoking member function. Equivalent to b.operator*(factor) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 14

  15. C++ Program With Overloaded Operators C++ Program With Overloaded Operators IIT Bombay int main() { V3 vel, acc, pos; V3 currDispl, currPos; double t, deltaT, totalT; Some code here while (t <= totalT) { currDispl = (vel * t) + 0.5 * (acc * (t*t)); currPos = currDispl + pos; t = t + deltaT; } Some code here } Invoking member function Legitimate C++ code operator* Invoking non-member function operator* Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 15

  16. Operators That Can Be Overloaded Operators That Can Be Overloaded IIT Bombay Almost all operators that you care about Binary: + - * / % ^ & | < > == != <= >= << >> && || = += -= *= /= %= ^= &= |= <<= >>= [] Note the assignment operators Unary: + - * & ! ~ ++ -- Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 16

  17. Assignment Operator Assignment Operator IIT Bombay Unlike several other operators, the assignment operator (=) is defined for all classes/structures V3 a(1.0, 2.0. 3.0); V3 b; b = a; members of a to corresponding Copy values of all data data members of b Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 17

  18. Assignment Overloading Assignment Overloading IIT Bombay We can re-define the assignment operator for a class/struct by defining the member function operator= (lhs = rhs) as an assignment expression is equivalent to lhs.operator=(rhs) Definition of member function operator=similar to copy constructor, except that operator= must also return a value (like all assignment expressions) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 18

  19. Assignment Overloading Example [Ref Assignment Overloading Example [Ref AGRBook AGRBook] ] IIT Bombay class Queue{ private: int front, nWaiting, elements[100]; public: Queue & operator=(const Queue &rhs) { front = rhs.front; nWaiting = rhs.nWaiting; for (int i = front, j = 0; j < nWaiting; j++) { elements[i] = rhs.elements[i]; i = (i + 1) % 100; } return *this; } Other member functions }; Inside a member function, this denotes a pointer to the receiver object Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 19

  20. Summary Summary IIT Bombay Operator overloading in C++ as a programming convenience Assignment overloading as a special case of operator overloading Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 20

More Related Content