Understanding bool functions in product comparison?

  • Thread starter Thread starter torquerotates
  • Start date Start date
  • Tags Tags
    Confused Functions
Click For Summary
SUMMARY

The forum discussion centers on the implementation of the member function bool Product::is_better_than(Product b) const in C++. The function compares two products based on their price and score, returning true if the score-to-price ratio of the current product is greater than that of the other product. The confusion arises from the interpretation of the comparison in the last line, which is clarified as a straightforward boolean evaluation.

PREREQUISITES
  • C++ programming language
  • Understanding of object-oriented programming concepts
  • Basic knowledge of boolean logic
  • Familiarity with class member functions
NEXT STEPS
  • Study C++ class member function syntax and usage
  • Learn about operator overloading in C++
  • Explore advanced C++ concepts such as templates and polymorphism
  • Investigate performance implications of different comparison strategies
USEFUL FOR

Software developers, particularly those working with C++ and object-oriented programming, as well as students learning about product comparison algorithms in programming contexts.

torquerotates
Messages
207
Reaction score
0

Homework Statement



I'm given the member function:

bool Product::is_better_than(Product b) const
{
if(price==0) return true;
if (b.price price==0) return false;
return score/price>b.score/b.price;
}

I'm really confused about the last line "return score/price>b.score/b.price;". Does it mean, return true if ">" holds and return false if "<" doesn't hold?
 
Physics news on Phys.org
Yes. You can think of all comparisons as functions that return true or false depending on whether the condition is satisfied.
 
oh I see. Thanks
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K