Lec15 - Lecture 15 Member Functions PIC 10A Todd Wittman...

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

View Full Document Right Arrow Icon
1 Lecture 15: Member Functions PIC 10A Todd Wittman The Product Class s Last class we introduced the Product class. It’s declaration looks like: class Product { public: Product ( ); void read ( ); bool is_better_than (Product b) const; void print ( ) const; private: string name; double price; int score; }; s We defined the Read member function. Print is simple. s Now let’s look at is_better_than. Review •public vs. private •member functions •constructor •accessors •mutators •const •encapsulation •That weird };
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 The Product Class s To find the “bang for the buck” ratio, compute score/price. s Return true if the current product has better ratio than a passed product b. Be sure to avoid division by zero! bool Product::is_better_than(Product b) const { if (b.price==0) return false; if (price==0) return true; return score/price > b.score/b.price; } s To call this function for Products a & b, use something like: if ( a.is_better_than(b) ) cout<<“Buy a!”; s Note how we access the different variables. To get the price of
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/27/2010 for the course PIC 157-050-21 taught by Professor Wittman during the Fall '10 term at UCLA.

Page1 / 5

Lec15 - Lecture 15 Member Functions PIC 10A Todd Wittman...

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

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