1 / 16

CMSC 202

CMSC 202. Operator Overloading I Lesson 11. // Employee class ____________________ ____________________ ____________________ ____________________ ____________________ ____________________ ____________________ ____________________. // Manager class ____________________

ova
Download Presentation

CMSC 202

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 202 Operator Overloading I Lesson 11

  2. // Employee class ____________________ ____________________ ____________________ ____________________ ____________________ ____________________ ____________________ ____________________ // Manager class ____________________ ____________________ ____________________ ____________________ ____________________ ____________________ ____________________ ____________________ Warmup Write the class declarations An Employee “has a” Manager (include >= 1 method and >=1 data member)

  3. Review • What is aggregation? • Give me an example of aggregation… • What is instantiation? • What would it look like to instantiate an Employee? • A Manager? • What might it look like to assign a Manager to an Employee?

  4. Let’s Take a Closer Look… // In Employee.h class Employee { public: void SetManager(const Manager& boss); private: Manager m_boss; }; // In Employee.cpp void Employee::SetManager(const Manager& boss) { m_boss = boss; } // In main… Employee me; Manager boss; me.SetManager(boss); Does this work? If so, how???

  5. Assignment Operator • Compiler creates a default assignment operator • Copies data member values Manager a ---------------- Name = “Bob” Manager a(“Bob”); Manager b; b = a; Manager b ---------------- Name = “” Manager b ---------------- Name = “Bob” Not the same Manager! Just have same data! Copy data

  6. Other Operators? • Does this work with other operators? Money a(2, 50); // 2.50 Money b(3, 20); // 3.20 Money c; c = a + b; • Unfortunately, no… • But…we can define it ourselves!

  7. Review: Function Overloading void swap (int& a, int& b); void swap (double& a, double& b); void swap (Bob& a, Bob& b); • Same (or similar) functionality for different types… • Function signatures include • Function name • Parameter list (both number and types) • Sidenote • C++ compiler has a built-in function called “swap”

  8. Closer Look at Operators… • We could do… Money a(2, 50); // 2.50 Money b(3, 20); // 3.20 Money c; c = Add(a, b); // we write… • Or…we can use • Operator Overloading and do this: c = a + b; // we write…

  9. Operator Overloading • Define a function that overloads an operator to work for a new type • Example: const Money operator+ (const Money& a, const Money& b) { return Money( a.GetDollars() + b.GetDollars(), a.GetCents() + b.GetCents()); } Function Name…essentially How could this function be improved? What’s going on here?

  10. Operator Overloading • Can also be overloaded as member functions • First object in statement becomes the “calling” object • a + bis equivalent toa.operator+(b) • Example: const Money Money::operator+ (const Money& b) const { return Money( m_dollars + b.m_dollars, m_cents + b.m_cents); } One parameter! Why const? Notice:implicit object!

  11. Return by const value? const Money operator+ (const Money& a, const Money& b); const Money operator+ (const Money& b) const; • Why return by const value? • Imagine this • Money a( 4, 50 ); • Money b( 3, 25 ); • Money c( 2, 10 ); • (a + b) = c; Why is this an issue? Think about: Money d;d = (a + b) = c; What is this supposed to mean? (d gets c’s value) Return by const value prevents us from altering the returned value… Evaluates to an unnamed object if we don’t return by const!

  12. Why not return by const-ref? const Money operator+ (const Money& a, const Money& b) { return Money( a.GetDollars() + b.GetDollars(), a.GetCents() + b.GetCents()); } • Look closely… • We return a copy of a temporary Money object… • It goes out of scope when the function returns!

  13. What about the following: Money a( 3, 25 ); Money d = a + 10; What does the compiler do? Looks for a constructor for Money that takes 1 int parameter Uses that constructor to build a new Money object Calls the ‘+’ operator function/method What about the following: Money a( 3, 25 ); Money d = 10 + a; class Money { public: Money( int dollars, int cents ); Money( int dollars ); // more methods private: int m_dollars; int m_cents; }; Operator Overloading Tries to find an int constructor that accepts a Moneyparameter…uh oh!

  14. Other Operators? • You can overload just about anything, but you should be VERY careful… • [ ] • * multiplication, pointer dereference • / division • + addition, unary positive • - substraction, unary negative • ++ increment, pre and post • -- decrement, pre and post • = assignment • <=, >=, <, >, ==, != comparisons • … • Many, many others…

  15. Let’s overload the multiplication on money… Ignore “roll-over” Member function? Non-member function? // In Money.h class Money { public: Money( int dollars, int cents ); int GetDollars(); int GetCents(); void SetDollars( int dollars ); void SetCents( int cents ); private: int m_dollars; int m_cents; }; // In main… Money m( 100, 00 ); m = m * 10; Practice

  16. Challenge • Fix the multiplication operator so that it correctly accounts for rollover.

More Related