Write a program to overload <= operator.
Sep 5, 2019Source code:
//program to overload <= operator
#include <iostream>
using namespace std;
class Fraction{
private:
int num;
int den;
public:
Fraction(int n = 0, int d = 0){
num = n;den = d;
}
//overloading operator with member function
bool operator <=(Fraction f){
//make conversion for data type
float self = static_cast<float>(num)/den;
float another = static_cast<float>(f.num)/f.den;
if(self <= another){
return true;
}
return false;
}
void display(){
cout<<num<<"/"<<den<<endl;
}
};
int main(){
Fraction f1(1,2), f2(2,4), f3(1,5);
if(f1 <= f2){
cout<<"Fraction f1 is less than or equal to f2"<<endl;
}
if(f1 <= f3){
cout<<"Fraction f1 is not less than or equal to f3"<<endl;
}
return 0;
}
Sample run:
Fraction f1 is less than or equal to f2