Fraction Class, == equals, << ostream operator - C++ Class

C++ examples for Class:Operator Overload

Description

Fraction Class, == equals, << ostream operator

Demo Code

                                                
#include <iostream>
using namespace std;
class Fraction {//w  w  w .j a  v  a2 s.  c  om
   private:
   int num, den;             // Numerator and denominator.
   public:
   Fraction() {set(0, 1);}
   Fraction(int n, int d)  {set(n, d);}
   Fraction(int n)  {set(n, 1);}
   Fraction(const Fraction &src);
   void set(int n, int d)
   {num = n; den = d; normalize();}
   int get_num() const {return num;}
   int get_den() const {return den;}
   Fraction add(const Fraction &other);
   Fraction mult(const Fraction &other);
   Fraction operator+(const Fraction &other)
   {return add(other);}
   Fraction operator*(const Fraction &other)
   {return mult(other);}
   bool operator==(const Fraction &other);
   friend ostream &operator<<(ostream &os,
   Fraction &fr);
   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 f1(1, 2);
   Fraction f2(1, 3);
   Fraction f3 = f1 + f2 + 1;
   cout << "1/2 + 1/3 + 1 = " << f3 << endl;
   return 0;
}
Fraction::Fraction(Fraction const &src) {
   num = src.num;
   den = src.den;
}
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 Multiple
   //
   int Fraction::lcm(int a, int b){
      int n = gcf(a, b);
      return a / n * b;
   }
   Fraction Fraction::add(const Fraction &other) {
      int lcd = lcm(den, other.den);
      int quot1 = lcd/den;
      int quot2 = lcd/other.den;
      return Fraction(num * quot1 + other.num * quot2,
      lcd);
   }
   Fraction Fraction::mult(const Fraction &other) {
      return Fraction(num * other.num, den * other.den);
   }
   bool Fraction::operator==(const Fraction &other) {
      return (num == other.num && den == other.den);
   }
   ostream &operator<<(ostream &os, Fraction &fr) {
      os << fr.num << "/" << fr.den;
      return os;
}

Result


Related Tutorials