{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture9CS32Nachenburg

# lecture9CS32Nachenburg - 1 We sday Fe dne bruary 3 C ustomC...

This preview shows pages 1–6. Sign up to view the full content.

1 Wednesday, February 3 rd CustomComparison Operators Templates TheStandard TemplateLibrary (STL) STL Iterators STL Algorithms (find and find_if)

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

View Full Document
2 Recursion Challenge Thefollowing function iteratively computes x n : int exponentiate(int x, int n) { int i, result = 1; for(i=0; i<n; i++) result *= x; return result; } Writea function to do this recursively.
3 Custom Comparison Operators Consider thefollowing main function that compares various objects to each other… Noticethat theway wecompare two dogs (by weight) is different than the way we comparetwo circles (by radius) . main() { int i1 = 3, i2 = 5; if (i1 > i2) cout << “i1 is bigger”; Circ a(5), b(6); if (a. radius () > b. radius ()) cout << “a was bigger”; Dog fido(10), spot(20); if (fido. weight () > spot. weight ()) cout << “fido is bigger”; } Wouldn’t it beniceif wecould compare objects like circles and dogs just likewecomparetwo integers? if (a > b) if (fido > spot) cout << “fido is bigger”; We can! Let’s seehow!

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

View Full Document
4 Theway to solvethis is to definea comparison function for each data type… bool operator > (const Dog &a, const Dog &b) { if (a. weight () > b. weight ()) return(true); else return(false); } bool operator > (const Circ &a,const Circ &b) { if (a. radius () > b. radius ()) return(true); else return(false); } Once defined, you can use theoperator in your program! main() { Dog fido(5), spot(3); if (fido > spot) cout << “fido wins”; Circ c(3), d(2); if (c > d) cout << “c is bigger”; } Custom Comparison Operators And of course, you can defineyour own versions of other comparison operators too… bool operator == (const Dog &a, const Dog &b) { if (a. weight () == b. weight ()) return(true); else return(false); } bool operator != (const Dog &a, const Dog &b) { if (a. weight () != b. weight ()) return(true); else return(false); } bool operator >= (const Dog &a, const Dog &b) { if (a. weight () >= b. weight ()) return(true); else return(false); } = fido weight 5 spot weight 3 Simply using the>= operator causes C++ to call your comparison function! 5 3 All comparison operators must return a Boolean value: true or false All comparison operators accept two const reference parameters. These refer to the two objects being compared. (While making them const isn’t always required, leaving it out can cause compiler errors!) Carey says: Similarly, Dog’s weight() function must be const. class Circ { public: int radius() const { return(m_rad); } ... private: int m_x,m_y,m_rad; }; Carey says: Oh, and by theway… since a and b are const, Circ’s radius() function now needs to be const too or else you’ll get an error!
5 Templates In this code, we’vewritten several different swap functions that swap thetwo values passed into thefunction. // the old way void SwapCircle (Circ &a, Circ &b) { Circle temp; temp = a; a = b; b = temp; } void SwapDog (Dog &d1, Dog &d2) { Dog temp; temp = a; a = b; b = temp; } main() { Circle a(5), b(6); Dog c(100), d(750); SwapCircle (a,b); SwapDog (c,d); } Wouldn’t it be nice if we could write one swap function and haveit work for any data type?

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 46

lecture9CS32Nachenburg - 1 We sday Fe dne bruary 3 C ustomC...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online