Answer to Question #206497 in C++ for vinayak.R

Question #206497

Create class fraction with necessary member variables and member functions .Overload + operator to add two fractions.Also overload << and >> operator to input and print fraction .


1
Expert's answer
2021-06-13T10:24:42-0400
#include<iostream>
using namespace std;
class Fraction{
private:
	float first, second;
public:
	Fraction(){
		
	}
	Fraction(float f, float s){
		first = f;
		second = s;
	}
	Fraction operator + (const Fraction& f){
		Fraction fr;
		fr.first = this->first + f.first;
		fr.second = this->second + f.second;
		return fr;
	}
	//Overloading << operator 
friend ostream& operator<<(ostream& b, Fraction &outer)
{
//	Fraction b;
//b<<"The first fraction is\n";
b <<"The answer is ("<< outer.first <<" , "<<outer.second <<")\n";






return b;
}
friend istream& operator>>(istream& inner, Fraction& fr)
{
   cout<<"Enter first\n";
	inner>>fr.first;
	cout<<"Enter second\n";
	inner>>fr.second;
	return inner;




}
};
int main(){
	Fraction F, P,  X;
	cout<<"Enter the first fraction"<<endl;
	cin>>F;
	Fraction T(F);
	cout<<"Enter the second fraction"<<endl;
	cin>>P;
 Fraction	M(P);
	X = M + T;
	cout<<X;
	
	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog