CSIT Object Oriented Programming With C++ (Overloading Comparison Operator)
In this post, I will teach you to overload Comparison operator. Comparison operator are used to compare two values and its result is always true or false. So operator function must return an integer value 1(if true) or 0 (if false). We can overload greater than operator(>) as follows. //Overloading comparison (greater than) operator #include<iostream> using namespace std; class Distance { int feet,inch; public: void getdata() { cout<<"Enter Distance: "; //feet first then inch cin>>feet>>inch; } int operator > (Distance d) { int fd,sd;//first distance and second distance fd=feet*12+inch; sd=d.feet*12+d.inch; if(fd>sd) return 1; else return 0; } }; int main() { Distance d1,d2; d1.getdata(); d2.getdata(); if(d1>d2) cout<<"d1 is greater than d2"<<endl; else cout<<"d2 is greater than d1"<<endl; return 0; } In this way we can overload Comparison( greater than) operator. We can also do it in same ways ...