Fraction class with member Functions - C++ Class

C++ examples for Class:Member Function

Description

Fraction class with member Functions

#include <cstdlib>
class Fraction {
   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;}
   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.
};
void Fraction::normalize(){
   if (den == 0 || num == 0) {
      num = 0;
      den = 1;
   }
   if (den < 0) {
      num *= -1;
      den *= -1;
   }
   int n = gcf(num, den);
   num = num / n;
   den = den / n;
}
int Fraction::gcf(int a, int b){
   if (b == 0)
      return abs(a);
   else
      return gcf(b, a%b);
   }
   // Lowest Common Multiple
   int Fraction::lcm(int a, int b){
      int n = gcf(a, b);
      return a / n * b;
}

Related Tutorials