Fraction Class with inline member function - C++ Class

C++ examples for Class:Member Function

Description

Fraction Class with inline member function

Demo Code

#include <iostream>
#include <string>
using namespace std;
class Fraction {//from  ww  w.  j a  v  a2  s  .  c om
   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.
};
int main()
{
   int a, b;
   string str;
   Fraction fract;
   while (true) {
      cout << "Enter numerator: ";
      cin >> a;
      cout << "Enter denominator: ";
      cin >> b;
      fract.set(a, b);
      cout << "Numerator is   " << fract.get_num()
      << endl;
      cout << "Denominator is " << fract.get_den()
      << endl;
      cout << "Do again? (Y or N) ";
      cin >> str;
      if (!(str[0] == 'Y' || str[0] == 'y'))
         break;
      }
      return 0;
   }
   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);
      }
   }
   int Fraction::lcm(int a, int b){
      int n = gcf(a, b);
      return a / n * b;
}

Result


Related Tutorials