Lec14OperatorOverloading_6per.pdf

Lec14OperatorOverloading_6per.pdf - EECS 402 Consider This...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 The University Of Michigan Andrew M. Morgan EECS402 Lecture 14 Savitch Ch. 8 Operator Overloading Returning By Constant Value EECS 402 EECS 402 Andrew M Morgan 2 Consider This Program class ChangePocketClass { public: ChangePocketClass():quarters(0),dimes(0) {} ChangePocketClass(int q, int d): quarters(q), dimes(d) {} void setQuarters(int val) { quarters = val; } void setDimes(int val) { dimes = val; } int getQuarters() { return (quarters); } int getDimes() { return dimes; } private: int quarters; int dimes; }; From main: ChangePocketClass c1; ChangePocketClass c2; ChangePocketClass c3; c1.setQuarters(5); c1.setDimes(7); c2.setQuarters(3); c2.setDimes(8); c3 = c1 + c2; EECS 402 EECS 402 Andrew M Morgan 3 Program Discussion What does the line "c3 = c1 + c2;" do? Logically, you would expect it to add the number of quarters in the c1 object to the number of quarters in the c2 object, and add the dimes as well Would you expect that adding two objects of any class works this way? (A member-by-member addition?) No For example, adding two "PersonClass" objects together doesn't make sense Due to this, addition is not provided by default for user-defined classes Therefore, the compiler will not allow the addition (yet) EECS 402 EECS 402 Andrew M Morgan 4 Using Operators While the operator "+" makes perfect sense for types such as int and float, what does it mean for user-defined data types? The programmer must define it, and implement it when appropriate Often, it doesn’t make any sense to perform mathematical operations on user-defined class types Rather than use the "+" operator, programmer could write a member function named "add": ChangePocketClass ChangePocketClass::add(const ChangePocketClass &rhs) const; Users aren't used to writing "c3 = c1.add(c2);" to add two items "c3 = c1 + c2;" would make a lot more sense, and wouldn't require a user to look up a function prototype to determine how to add two objects together EECS 402 EECS 402 Andrew M Morgan 5 Using Operators With User-Defined Data Types C++ allows a programmer to customize common operators work for different classes When C++ comes across an operator for user-defined data types, a function is called to perform the operation The "+" operator results in a call to a function named "operator+" The "=" operator results in a call to a function named "operator=" etc The following list of operators can be overloaded: + - * / % ^ & | ~ ! = < > += -= *= ?= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete new[] delete[] Note: Just because you can overload an operator doesn't necessarily mean it is a good idea EECS 402 EECS 402 Andrew M Morgan 6 Overloading Operators, Example class ChangePocketClass { public: ChangePocketClass(): quarters(0),dimes(0) {} ChangePocketClass(int q, int d): quarters(q), dimes(d) {} ...no change to readers/writers ChangePocketClass operator+(const ChangePocketClass in) { ChangePocketClass result; result.quarters = quarters + in.quarters; result.dimes = dimes + in.dimes; return (result); } private: int quarters; int dimes; };
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 EECS 402 EECS 402 Andrew M Morgan 7 Output Of Main int main() { ChangePocketClass c1; ChangePocketClass c2; ChangePocketClass c3; c1.setQuarters(5); c1.setDimes(7); c2.setQuarters(3);
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern