lecture9

lecture9 - 1 We sday, Fe dne bruary 2 nd C ustomC parison...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Wednesday, February 2 nd Custom Comparison Operators Templates The Standard Template Library (STL) STL Iterators STL Algorithms (find and find_if)
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Recursion Challenge The following 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; } Write a function to do this recursively.
Background image of page 2
3 Generic Programming In this lecture, we’re going to learn about “Generic Programming” The goal of GP is to build algorithms that are able to operate on many different types of data (not just a single type). For example, a sort function that doesn’t just sort ints but can sort strings , ints Student objects , etc. Or a linked list class that doesn’t just hold Students , but can hold ints Robots Once you define such a generic function or class, you can quickly reuse it to solve many different problems .
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Part 1: Allowing Generic Comparisons Consider the following main function that compares various objects to each other… Notice that the way we compare two dogs (by weight) is different than the way we compare two 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 be nice if we could compare objects like circles and dogs just like we compare two integers? if (a > b) if (fido > spot) cout << “fido is bigger”; We can! Let’s see how!
Background image of page 4
5 The way to solve this is to define a 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. return(true); else return(false); } Once defined, you can use the operator in your program! 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 define your 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); } = 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; }; Oh, and by the way… since a and b are const, Circ’s radius() function now needs to be const too or else you’ll get an error!
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Part 2: Writing Generic Functions In this code, we’ve written several different swap functions that swap the two values passed into the function.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 45

lecture9 - 1 We sday, Fe dne bruary 2 nd C ustomC parison...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online