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 .
#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;
}
Comments
Leave a comment