C ++> = operator overloading with two different return values
I have a class that looks like this:
class Player
{
friend bool operator>=(Player &pl1, Player &pl2)
{
return true;
};
private:
int nr, height, weight;
}
The player has quantity, height and weight. Now I would like to know if Player1 is bigger and / or heavier than Player2. After that, I would like to print it like this:
Player A is 2 cm bigger (smaller) and 7 kg heavier (lighter) than Player B.
How do I manage when I can only return true or false? I can go back when it gets bigger and heavier or fake, when it gets smaller and lighter, but how do I manage the larger / lighter | smaller / severe case?
EDIT: I HAVE TO DO WITH operator>=
. This is an exam for my school, and the condition is to do it like this. Here is the text:
Once entered, the players will be displayed. When using the operator overload> =, it will be checked if the player is larger and / or heavier than the other player. The result is displayed with specific data, for example: Player A is 2cm larger (less) and 7kg heavier (lighter) than Player B.
source to share
So, if you are ordering your class differently, perhaps you can do something like the following:
class PlayerWeight {
private:
int weight;
public:
int getWeight() const {
return weight;
}
bool operator >=( const PlayerWeight &playerWeight ) {
return weight >= playerWeight.getWeight();
}
PlayerWeight( int weight ) : weight( weight ) {
}
};
class PlayerHeight {
private:
int height;
public:
int getHeight() const {
return height;
}
bool operator >=( const PlayerHeight &playerHeight ) {
return height >= playerHeight.getHeight();
}
PlayerHeight( int height ) : height( height ) {
}
};
class Player : public PlayerHeight, public PlayerWeight {
public:
PlayerHeight& height() {
return *this;
}
PlayerWeight& weight() {
return *this;
}
Player( int height, int weight ) : PlayerHeight( height ), PlayerWeight( weight ) {
}
};
int main( int argc, char**argv ) {
Player playerA( 72, 180 ), playerB( 74, 160 );
// comparison
std::cout << "Player A is " << ( (PlayerHeight)playerA >= playerB ? "bigger" : "smaller" ) << " and " << ( (PlayerWeight)playerA >= playerB ? "heavier" : "lighter" ) << std::endl;
// or...
std::cout << "Player A is " << ( playerA.height() >= playerB ? "bigger" : "smaller" ) << " and " << ( playerA.weight() >= playerB ? "heavier" : "lighter" ) << std::endl;
return 0;
}
source to share
If strict ordering does not exist for your classes, do not define ordering operators.
Instead, you can write functions like heightDifference
and weightDifference
:
int heightDifference (const Player& a, const Player& b)
{
return a.height - b.height;
}
You can then use these functions to design the information you need.
source to share
You can also try the typical approach to optimization problems.
When you have multiple (conflicting) goals, you can try taking a scalar aggregation / prediction of your values.
those. you have two different goals, to maximize weight and height ( >=
could be interpreted as an impressive statement!):
double impressive(int height, int weight)
{
return height + ratio * weight; // some good ratio
// lexicographic ordering induced by very large/small values of ratio.
}
bool operator>=(Player &a, Player &b)
{
return impressive(a.height, a.weight) >= impressive(b.height, b.weight);
};
It all depends on the semantics of the operator.
source to share
You can make a comparison function that returns std::pair
where one will give you the difference in height and the other will give you the difference in weight.
using namespace std;
pair<int,int> compare(Player &a, Player &b)
{
return make_pair(a.height-b.height, a.weight-b.weight);
}
Finally, you can compare your results by simply putting if-else
on what you want.
source to share
The problem with order operators ( <=
) is that ordinary mortals use them as a full order relation: antisymmetric, reflexive, and transitive, and specific to each pair. If any of these 4 rules are wrong, do not use <=
.
If you need a complete order that meets your requirement (or at least does not contradict it), I would advise:
- if A if is (strictly) greater than B then B <= A (actually B <A)
- if A is the same size as B and A is heavier (or the same weight as B), then B <= A
- else A <= B (actually A <B)
This means that you are combining your two-order relationship with priority.
source to share