Question: C++ b) Implement the operator^ and operator^= functions for Fraction class. (You'll only be able to use fairly small test values for parameter p, due
C++
b) Implement the operator^ and operator^= functions for Fraction class. (You'll only be able to use fairly small test values for parameter p, due to the limited range of data type int.).
// HEADER TEMPLATE //
#ifndef FRACTION_H #define FRACTION_H #includeclass Fraction { friend std::ostream& operator<<(std::ostream& out, const Fraction& frac); friend std::istream& operator>>(std::istream& in, Fraction & frac); private: int n, d; static int getGCD(int b, int c); // return gcd of b and c public: Fraction(int n1 = 0, int d1 = 1); // constructor Fraction& operator+=(const Fraction& other); Fraction operator+(const Fraction& other) const; Fraction& operator-=(const Fraction& other); Fraction operator-(const Fraction& other) const; Fraction operator-() const; // unary - Fraction& operator*=(const Fraction& other); Fraction operator*(const Fraction& other) const; Fraction& operator/=(const Fraction& other); Fraction operator/(const Fraction& other) const; Fraction& operator++(); // pre increment Fraction operator++(int); // post increment Fraction& operator--(); // pre decrement Fraction operator--(int); // post decrement bool operator<(const Fraction& other) const; bool operator<=(const Fraction& other) const; bool operator>(const Fraction& other) const; bool operator>=(const Fraction& other) const; bool operator==(const Fraction& other) const; bool operator!=(const Fraction& other) const; Fraction operator^ (int p) const; // p > 0: take n^p/d, p<0 take d^pn, p == 0--> 1/1) Fraction& operator^=(int p); void printValue() const; }; #endif
// IMPLEMENTATION TEMPLATE //
#include "Fraction.h" #includeusing namespace std; int Fraction::getGCD(int b, int c) { if (b < 0) b = -b; if (c < 0) c = -c; while (c != 0) { int temp = b; b = c; c = temp % c; } // EXAMPLE b = 12, c = 7 // temp = 12, b = 7, c = 5 // temp = 7, b = 5, c = 2 // temp = 5, b = 2 , c =1 // temp = 2, b = 1, c = 0 // Now b is the gcd. return b; } Fraction::Fraction(int n1 , int d1 ) { if (d1 == 0) d1 = 1; else if (d1 < 0) { // suppose n1 = -10, d1 =-6 n1 = -n1; d1 = -d1; // Now n1 = 10 and d1 = 6 } int g = getGCD(n1, d1); // g = 2 since the gcd of 10 and 6 is 2. n = n1 / g; // Set n to 10 / 2 --> 5 d = d1 / g; // Set d to 6 / 2 --> 3 // So the internal representation of the client's fraction is 5/3 } void Fraction::printValue() const { cout << n << "/" << d << endl; } Fraction Fraction::operator-() const { Fraction temp(-n , d ); return temp; } Fraction& Fraction::operator+=(const Fraction& other) { *this = *this + other; return *this; } Fraction Fraction::operator+(const Fraction& other) const { // Remember, to add two fraction we first get a common denominator. // After that, we add revised numerators and divide by the common denominator. // Example: 1/2 + 1/3 --> 3/6 + 2/6 --> (3 + 2) / 6 --> 5 / 6 // Note: The common denominator can be taken as the product of the // two denominators. Then the first "revised" numerator will be multiplied // by the second denominator and the second revised numerator will be multiplied // by the first denominator (cross multiply). // Example 3/4 + 5/6 --> (3 * 6) / (4 * 6) + (5 * 4) / (4 * 6) -->((3 *6 ) + (5 *4)) / (4 * 6) int d1 = d * other.d; int n1 = n * other.d + other.n * d; Fraction temp(n1, d1); // the Constructor will do the job of reducing the Fraction for us. return temp; // return Fraction( n * other.d + other.n * d, d * other.d) ; // shorter version of the lines above. }
Please provide code for Fraction.h, Fraction.cpp, and Driver.cpp
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
