Comparing objects using if else | Operator Overload

I need compare(>,<,==)two class objectbased on the different criteria described below.

class Student
{
    int iRollNumber;
    int iSection;
    int iMarks;
}
  • I want to make a comparison with iRollNumber, iSection, iMarks(independently).
  • I want to make a comparison with iRollNumber, iSection(Combined).
  • I want to make a comparison with iMarks, iSection(Combined).
  • ..........

I am currently achieving this with help GetMethods()and comparing them using structure if elseif elseif...

This leads to messy code everywhere!

If I use operator overloading, I have to solve one of the comparison methods.

Please suggest a way to do this with elegant coding.

or

Is it possible to cause operator overload Polymorphically?

+3
source share
1 answer

Write named functions:

int CompareR( const Student & a, const Student & b );
int CompareS( const Student & a, const Student & b );
int CompareM( const Student & a, const Student & b );
int CompareRS( const Student & a, const Student & b );
int CompareMS( const Student & a, const Student & b );

- . , strcmp():

<  returns -1
== returns 0
>  returns 1
+7

All Articles