Fraction class Arithmetic member function: add and mult - C++ Class

C++ examples for Class:Member Function

Description

Fraction class Arithmetic member function: add and mult

Demo Code

#include <iostream>
using namespace std;
class Fraction {//ww w. j a va  2 s  . c o m
   private:
   int num, den;               // Numerator and denominator.
   public:
   void set(int n, int d)
   {num = n; den = d; normalize();}
   int get_num()  {return num;}
   int get_den()  {return den;}
   Fraction add(Fraction other);
   Fraction mult(Fraction other);
   private:
   void normalize();   // Convert to standard form.
   int gcf(int a, int b);  // Greatest Common Factor.
   int lcm(int a, int b);  // Lowest Common Denom.
};
int main()
{
   Fraction fract1, fract2, fract3;
   fract1.set(1, 2);
   fract2.set(1, 3);
   fract3 = fract1.add(fract2);
   cout << "1/2 plus 1/3 = ";
   cout << fract3.get_num() << "/" << fract3.get_den()
   << endl;
   return 0;
}
void Fraction::normalize(){
   if (den == 0 || num == 0) {
      num = 0;
      den = 1;
   }
   // Put neg. sign in numerator only.
   if (den < 0) {
      num *= -1;
      den *= -1;
   }
   // Factor out GCF from numerator and denominator.
   int n = gcf(num, den);
   num = num / n;
   den = den / n;
}
// Greatest Common Factor
int Fraction::gcf(int a, int b) {
   if (b == 0) {
      return abs(a);
   } else {
      return gcf(b, a%b);
   }
}
// Lowest Common Denominator
int Fraction::lcm(int a, int b){
   int n = gcf(a, b);
   return a / n * b;
}
Fraction Fraction::add(Fraction other) {
   Fraction fract;
   int lcd = lcm(den, other.den);
   int quot1 = lcd/den;
   int quot2 = lcd/other.den;
   fract.set(num * quot1 + other.num * quot2, lcd);
   return fract;
}
Fraction Fraction::mult(Fraction other) {
   Fraction fract;
   fract.set(num * other.num, den * other.den);
   return fract;
}

Result


Related Tutorials