Below is Fraction.h which has the declaration of the Fraction class. The Fraction class stores rational fractions with a
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Below is Fraction.h which has the declaration of the Fraction class. The Fraction class stores rational fractions with a
Below is Fraction.h which has the declaration of the Fraction class. The Fraction class stores rational fractions with a numerator and denominator. Fraction.h: 1 2 4 5 6 7 S class Fraction { private: int num, den; // member variables public: // constructor Fraction(int n, int d = 1) { num = n; den d; } // operator overloads Fraction operator+(const Fraction rhs); friend Fraction operator+(const Fraction lhs, const Fraction rhs); friend ostream& operator<<(ostream& out, const Fraction a); }; Define the three operator overloads declared above (+, *, <<) in Fraction.cpp below. 9 10 11 12 13 14 Fraction.cpp: int main() { Fraction a(5, 8); Fraction b(4, 9); cout << (a * b) << endl; // prints 20/72 cout << (a + b) << endl; // prints 76/72 }
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!