Fraction class implementation

Hi Experts,

I need help on implementing some of my Fraction class functions. I am including the whole of it, I have implemented a

small part of it but need help on the remaining part.

Fraction.h is below

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485`` `````` #include using namespace std; class Fraction { public: Fraction(int = 1, int = 1); Fraction(const Fraction&); // DESTRUCTOR ~Fraction(); // BASIC ASSIGNEMENT OPERATOR Fraction& operator=(const Fraction&); // UNARY ARITHMETIC OPERATORS Fraction operator-(); // Operand 'this' implicit Fraction operator+(); Fraction& operator--(); Fraction& operator++(); Fraction operator--(int); Fraction operator++(int); // ADVANCED ASSIGNEMENT OPERATORS Fraction& operator+=(const Fraction&); Fraction& operator-=(const Fraction&); Fraction& operator*=(const Fraction&); Fraction& operator/=(const Fraction&); Fraction& operator%=(const Fraction&); // ARRAY INDEX OPERATOR Fraction const& operator[](unsigned int) const; // Read-only access Fraction& operator[](unsigned int); // Read/Write access: // CONVERSION OPERATORS operator int(); operator double(); // SPECIAL OPERATORS Fraction operator!(); // Inverts a fraction int operator*(); // Gets the Numerator unsigned int operator~(); // Gets the Denominator friend ostream& operator<<(ostream&, const Fraction&); friend istream& operator>>(istream&, Fraction&); // CONSTANTS OF DATATYPE static const Fraction sc_fUnity; // Defines 1/1 static const Fraction sc_fZero; // Defines 0/1 // STATIC UTILITY FUNCTIONS static const int precision() { return 1000000; }; static const Fraction sc_fUnity; // Defines 1/1 private: // DATA MEMBERS int iNumerator_; // The Numerator unsigned int uiDenominator_; // The Denominator // STATIC UTILITY FUNCTIONS static int gcd(int, int); // Finds the gcd for two +ve integers static int lcm(int, int); // Finds the lcm for two +ve integers }; ``````

Main.cpp is below:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161`` ``````#include using namespace std; #include "Fraction.h" #include // Test code for Fraction class int main() { Fraction f1(5, 3); Fraction f2(7); Fraction f3; Fraction f5(55, 33); // BASIC ASSIGNEMENT OPERATOR // Fraction& operator=(const Fraction&); cout << "Assingment (Before): f3 = " << f3 << ". f1 = " << f1 << endl; f3 = f1; cout << "Assingment (After): f3 = " << f3 << ". f1 = " << f1 << endl; f1 = Fraction(5, 12); // UNARY ARITHMETIC OPERATORS // Fraction operator-(); // Operand 'this' implicit f3 = -f1; cout << "Unary Minus: f3 = " << f3 << ". f1 = " << f1 << endl; // Fraction operator+(); // Fraction operator--(); // Pre-decrement. Dividendo f5 = Fraction::sc_fUnity; cout << "Pre-Decrement (Before): f3 = " << f3 << ". f5 = " << f5 << endl; f3 = --f1; cout << "Pre-Decrement (After): f3 = " << f3 << ". f1 = " << f1 << endl; // Fraction operator--(int); // Post-decrement. Lazy Dividendo f3 = Fraction::sc_fUnity; cout << "Post-Decrement (Before): f3 = " << f3 << ". f1 = " << f1 << endl; f3 = f1--; cout << "Post-Decrement (After): f3 = " << f3 << ". f1 = " << f1 << endl; // Fraction operator++(); // Pre-increment. Componendo f3 = Fraction::sc_fUnity; cout << "Pre-Increment (Before): f3 = " << f3 << ". f1 = " << f1 << endl; f3 = ++f1; cout << "Pre-Increment (After): f3 = " << f3 << ". f1 = " << f1 << endl; // Fraction operator++(int); // Post-increment. Lazy Componendo f3 = Fraction::sc_fUnity; cout << "Post-Increment (Before): f3 = " << f3 << ". f1 = " << f1 << endl; f3 = f1++; cout << "Post-Increment (After): f3 = " << f3 << ". f1 = " << f1 << endl; // ADVANCED ASSIGNEMENT OPERATORS // Fraction& operator+=(const Fraction&); f1 = Fraction(5, 12); f2 = Fraction(7, 18); f3 = f2; f2 += f1; cout << "+=: f2 = " << f2 << ". f1 = " << f1 << ". f2 (before) = " << f3 << endl; f3 = f2; f2 += f2; cout << "+=: f2 = " << f2 << ". f2 (before) = " << f3 << endl; // Fraction& operator-=(const Fraction&); f1 = Fraction(5, 12); f2 = Fraction(7, 18); f3 = f2; f2 -= f1; cout << "-=: f2 = " << f2 << ". f1 = " << f1 << ". f2 (before) = " << f3 << endl; f3 = f2; f2 -= f2; cout << "-=: f2 = " << f2 << ". f2 (before) = " << f3 << endl; // Fraction& operator*=(const Fraction&); f1 = Fraction(5, 12); f2 = Fraction(7, 18); f3 = f2; f2 *= f1; cout << "*=: f2 = " << f2 << ". f1 = " << f1 << ". f2 (before) = " << f3 << endl; f3 = f2; f2 *= f2; cout << "*=: f2 = " << f2 << ". f2 (before) = " << f3 << endl; // Fraction& operator/=(const Fraction&); f1 = Fraction(5, 12); f2 = Fraction(7, 18); f3 = f2; f2 /= f1; cout << "/=: f2 = " << f2 << ". f1 = " << f1 << ". f2 (before) = " << f3 << endl; f3 = f2; f2 /= f2; cout << "/=: f2 = " << f2 << ". f2 (before) = " << f3 << endl; // Fraction& operator%=(const Fraction&); f1 = Fraction(7, 18); f2 = Fraction(5, 12); f3 = f2; f2 %= f1; cout << "%=: f2 = " << f2 << ". f1 = " << f1 << ". f2 (before) = " << f3 << endl; f3 = f2; f2 %= f2; cout << "%=: f2 = " << f2 << ". f2 (before) = " << f3 << endl; // ARRAY INDEX OPERATOR // Fraction& operator[](unsigned int); Fraction f[2]; f1 = Fraction(7, 18); f2 = Fraction(5, 12); f[0] = f1; f[1] = f2; cout << "[]: f[0] = " << f[0] << ". f[1] = " << f[1] << endl; // CONVERSION OPERATORS // operator int(); f[0] *= 11; cout << "(int): (int)f[0] = " << (int)f[0] << ". f[0] = " << f[0] << endl; // operator double(); cout << "(double): (double)f[0] = " << (double)f[0] << ". f[0] = " << f[0] << endl; // SPECIAL OPERATORS // Fraction operator!(); // Inverts a fraction f[1] *= 11; cout << "Inversion: 1/f[1] = " << !f[1] << ". f[1] = " << f[1] << endl; // int operator&(); // Gets the Numerator f1 = Fraction(7, 18); cout << "Numerator: f1.Num = " << &f1 << ". f1 = " << f1 << endl; // unsigned int operator~(); // Gets the Denominator cout << "Denominator: f1.Deno = " << ~f1 << ". f1 = " << f1 << endl; // COMPOSITE SCENARIO f1 = Fraction(7, 18); f2 = Fraction(5, 12); f3 = Fraction(16, 9); f[0] = f1; f[1] = f2; Fraction fTmp; fTmp = f1 - f2++ * f3 + f[0]/f[1]; cout << "Composite: fTmp = " << fTmp << ". f1 = " << f1 << ". f2 = " << f2 << ". f3 = " << f3 << ". f[0] = " << f[0] << ". f[1] = " << f[1] << endl; return 0; } ``````

Fraction.cpp, that I have done a bit but need help is below

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include "Fraction.h" #include Fraction::Fraction(int n, int d):iNumerator_(n),uiDenominator_(d) { cout << "Normal Constructor (2 Params): " <<*this<<"\n"; } const Fraction Fraction::sc_fUnity(1,1); Fraction::Fraction(const Fraction& f):iNumerator_(f.iNumerator_),uiDenominator_(f.uiDenominator_) { cout <<"Copy constructor"<
Topic archived. No new replies allowed.