Write a OOP to overload "==" operator to determine the equality of two fractional numbers (fractional numbers must be in terms of numerator and denominator)

Sep 15, 2018

Program:

// program to overload the == operator  

#include<iostream>
using namespace std;

class Fraction{
	private:
		int numerator;
		int denominator;
	public:
		// member initialize list
		Fraction(int num, int den):numerator(num),denominator(den){}
		// defination of operator
		//usign boolean variable to check true or false
		bool operator ==(Fraction fr){
			if((numerator == fr.numerator) && (denominator == fr.denominator)){
				return true;
			}else{
				return false;
			}
		}
};


int main(){
	Fraction fr1(2,3),fr2(2,5),fr3(2,3);
	if (fr1 == fr2)
	{
		cout<<"fr1 and fr2 are equal:"<<endl;
	}else{
		cout<<"fr1 and fr2 are not equal:"<<endl;
	}
	if (fr1 == fr3)
	{
		cout<<"fr1 and fr3 are equal:"<<endl;
	}else{
		cout<<"fr1 and fr3 are not equal:"<<endl;
	}
	return 0;
}

Sample Run:

fr1 and fr2 are not equal:
fr1 and fr3 are equal:

 

Related


Tools/Apps





© Nepal Exchange Rates