Wednesday, August 4, 2010

Operator Overloading: fraction class

Here is a fraction class that overloads operators to for manipulating fraction objects. It is minimal and could be improved. For one thing it doesn't use a header file interface. (I wrote the whole class in "Fraction.h"). I didn't overload the equality and non equality operators, which I should have.

#include
using namespace std;

class Fraction
{
/************************************
* This class enables a user to do fraction
* math. To do so it overloads several operators.
* It also introduces some error trapping
*******************************************/
//public members
public:
//default constructor
Fraction(){}
//overloaded constructor
Fraction(int numerator, int denominator)
{
SetNumerator(numerator);
SetDenominator(denominator);
}

//start overloaded operators
Fraction operator+ (const Fraction &F2)
{
//this overloads the + operator for two
//fraction objects. It returns the result
//as a fraction object. It gets the greatest
//common denominator--better would be a routine
//that returns the lowest common denominator
//the "this->" pointer points to elements of
//the current class

Fraction temp; //declare an object of the type Fraction
temp.den=this->den * F2.den;
temp.num=F2.den* this->num + this->den * F2.num;
return temp;
}

Fraction operator-(const Fraction &F2)
{
//does the same thing as + but with a -
Fraction temp; //declare an object of the type Fraction
temp.den=this->den * F2.den;
temp.num=F2.den* this->num - this->den * F2.num;
return temp;
}

Fraction operator* (const Fraction &F2)
{
Fraction temp;
temp.den=this->den * F2.den;
temp.num=this->num * F2.num;
return temp;
}

Fraction operator/ (const Fraction &F2)
{
Fraction temp;
temp.den=this->den * F2.num;
temp.num=this->num * F2.den;
return temp;
}

//these next operators are different because they change
//the current instance of Fraction, so they
//return not a third object but a pointer to the
//current instance using *this

Fraction& operator= (const Fraction &F2)
{
this->den=F2.den;
this->num=F2.num;
return *this;
}

Fraction& operator+=(const Fraction &F2)
{
//again we have to get the greatest common denominator
this->num=F2.den * this->num + this->den * F2.num;
this->den=this->den + F2.den;
return *this;
}

Fraction& operator-=(const Fraction F2)
{
this->num=F2.den * this->num - this->den * F2.num;
this->den=this->den * F2.den;
return *this;
}

Fraction& operator*=(const Fraction F2)
{
this->num = this->num * F2.num;
this->den=this->den * F2.den;
return *this;
}

Fraction& operator/=(const Fraction F2)
{
this->num = this->num * F2.den;
this->den=this->den * F2.num;
return *this;
}

//print out the fraction
void PrintFraction()
{
cout << num <<"/" << den << endl;
}

//private members
private:
int num;
int den;

//set functions with some error trapping
//these are private because I don't want
//the user directly access them
void SetNumerator(int n)
{

num = n;
}

void SetDenominator(int d)
{
if (d==0)
throw "Division by 0 error";
den=d;
}
};


Here is some code for testing the fraction. Notice it also uses a try catch black to catch errors thrown by the fraction class:

#include "Fraction.h"

int main()
{
try
{
//uncomment this to see the error thrown
//Fraction e(2,0);
//Declare 3 fractions
Fraction a(1,2);
Fraction b(1,3);
Fraction c; //uses default constructor

//show the current fractions
cout << "Fraction a " << endl;
a.PrintFraction();
cout <<"Fraction b " << endl;
b.PrintFraction();

cout << "\nfraction a + fraction b = ";
c = a + b;
c.PrintFraction();

cout << "\nfraction a - fraction b = ";
c = a - b;
c.PrintFraction();

cout << "\nfraction a * fraction b = ";
c = a * b;
c.PrintFraction();

cout << "\nfraction a / fraction b = ";
c = a / b;
c.PrintFraction();

cout <<"\nfraction a += fraction b = ";
a += b;
a.PrintFraction();

cout << "\nfraction a -= fraction b = ";
a -= b;
a.PrintFraction();

cout << "\nfraction a *= fraction b = ";
a *= b;
a.PrintFraction();

cout << "\nfraction a /= fraction b = ";
a /= b;
a.PrintFraction();


cout <<"Fraction a currently ";
a.PrintFraction();
cout << "\nFraction b currently ";
b.PrintFraction();
cout << "\nAssigning Fraction a = Fraction b. Fraction a = ";
a = b;
a.PrintFraction();

}
catch (char* s)
{
//the char* is a pointer to the strin
//thrown from the set functions
cout << s << endl;
}
char x;
cin >> x;
}

No comments:

Post a Comment