FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!

C++ Opertator overloading




/* C++ Opertator overloading by Bertrand Brown */

/* START */
#include <iostream.h>
#include <conio.h>


//CLASS DECLERATION//
class Fraction
{
private:
int numerator;
int denominator;

public:
Fraction(int numer, int denom);

void store(int numer, int denom);

void Fraction::operator+(Fraction& t );
void Fraction::operator-( Fraction& t );
void Fraction::operator/( Fraction& t );
void Fraction::operator*( Fraction& t );

void Fraction::operator=( Fraction& t );
void Fraction::operator==( Fraction& t );
void Fraction::operator!=( Fraction& t );
void Fraction::operator>( Fraction& t );
void Fraction::operator<( Fraction& t );
void Fraction::operator>=( Fraction& t );
void Fraction::operator<=( Fraction& t );
};


//STORING OF FRACTIONS//
void Fraction::store(int numer, int denom)
{
numerator = numer;
denominator = denom;
}


//CLASS DEFINITION//
Fraction::Fraction(int numer, int denom)
{
//store and check for negative number
store(numer, denom);
if(denom < 0){ denominator = -denominator; }
if(numer < 0){ numerator = -numerator; }

//get highest common multiple
int temp = (numerator) * (denominator);
if (temp > 0)
{
for (temp;temp > 1;temp--)
{
//reduce fraction to simplest form - works perfectly
if ((numerator % temp == 0) && (denominator % temp == 0))
{
numerator /= temp;
denominator /= temp;
cout << "N = " << numerator << "D = " << denominator << "\n\n";
}
}
}
}

//COMMON CALCULATIONS//
void Fraction::operator+(Fraction& t)
{
int answern, answerd;

int a, b, c, d;
a = numerator;
b = denominator;
c = t.numerator;
d = t.denominator;

a = a * d; //numerator 1 * denominator 2
c = c * b; //numerator 2 * denominator 1
b = b * d; // denominator 1 * denominator 2
d = b; //denominator 2 = denominator 1

if (denominator == 0 && denominator == 0)
{
cout << "Answer is undefined \n\n";
}
else
{
answern = a + c;
answerd = d;
cout << "The sum is: " << answern << "/" << answerd << "\n\n";
}
}


void Fraction::operator-(Fraction& t)
{
int answern, answerd;

int a, b, c, d;
a = numerator;
b = denominator;
c = t.numerator;
d = t.denominator;

a = a * d; //numerator 1 * denominator 2
c = c * b; //numerator 2 * denominator 1
b = b * d; // denominator 1 * denominator 2
d = b; //denominator 2 = denominator 1

if (denominator == 0 && denominator == 0)
{
cout << "Answer is undefined \n\n";
}
else
{
answern = a - c;
answerd = d;
cout << "The subtraction is: " << answern << "/" << answerd << "\n\n";
}
}


void Fraction::operator*(Fraction& t)
{
int answern, answerd;

int a, b, c, d;
a = numerator;
b = denominator;
c = t.numerator;
d = t.denominator;

a = a*c; //numerator 1 * numerator 2
c = a; // numerator 2 = numerator 1
b = b * d; //denominator 1 * denominator 2
d = b; //denominator 2 = denominator 1

if (denominator == 0 && denominator == 0)
{
cout << "Answer is undefined \n\n";
}
else
{
answern = a;
answerd = b;
cout << "The multipl is: " << answern << "/" << answerd << "\n\n";
}
}


void Fraction::operator/(Fraction& t)
{
int answern, answerd;

int a, b, c, d;
a = numerator;
b = denominator;
c = t.numerator;
d = t.denominator;

a = a * d; //numerator 1 * denominator 2
d = a; //denominator 2 = to numerator 1
b = b * c; //denominator 1 * numerator 2
c = b; // numerator 2 = denominator 1

if (denominator == 0 && denominator == 0)
{
cout << "Answer is undefined \n\n";
}
else
{
answern = a;
answerd = b;
cout << "The Division is: " << answern << "/" << answerd << "\n\n";
}
}


//OTHER FUNCTIONS//
void Fraction::operator==(Fraction& t)
{
if(numerator == (t.numerator) && denominator == (t.denominator))
{
cout << "Both fractions are equal!\n\n";
}
else
{
cout << "Theses frcations are not equal!\n\n";
}
}

void Fraction::operator=(Fraction& t)
{
numerator = t.numerator;
denominator = t.denominator;
}


void Fraction::operator!=(Fraction& t)
{
if(numerator == (t.numerator) && denominator == (t.denominator))
{
cout << "Both fractions are still equal!\n\n";
}
else
{
cout << "Theses frcations are not equal!\n\n";
}
}


void Fraction::operator>(Fraction& t)
{
if(numerator > (t.numerator) && denominator >= (t.denominator))
{
cout << "the first fraction is greater than the second!\n\n";
}
else
{
cout << "the second fraction is greater than the first!\n\n";
}
}


void Fraction::operator<(Fraction& t)
{
if(numerator < (t.numerator) && denominator <= (t.denominator))
{
cout << "the first fraction is smaller than the second!\n\n";
}
else
{
cout << "the second fraction is smaller than the first!\n\n";
}
}



//greater or equal//

void Fraction::operator>=(Fraction& t)
{
if(numerator >= (t.numerator) && denominator >= (t.denominator))
{
cout << "the first fraction is greater than or equal to the second!\n\n";
}
else
{
cout << "the second fraction is greater than or equal to the first!\n\n";
}
}


void Fraction::operator<=(Fraction& t)
{
if(numerator <= (t.numerator) && denominator <= (t.denominator))
{
cout << "the first fraction is smaller than or equal to the second!\n\n";
}
else
{
cout << "the second fraction is smaller than or equal to the first!\n\n";
}
}


//MAIN OR CLIENT//
int main()
{
int a = 0, b = 0, c = 0, d = 0;
char x;

boot: //quick restart to get input
//get two fractions
cout << "Enter a fraction as N/D : ";
cin >> a;
cin >> x;
cin >> b;
x = '/';
cout << "Fraction is: " << a << x << b << "\n\n";

cout << "Enter another fraction as N/D : ";
cin >> c;
cin >> x;
cin >> d;
x = '/';
cout << "Fraction is: " << c << x << d << "\n\n";

if(b == 0 || d == 0)
{
cout << "Cannot accept 0 denominator! ...restart\n\n";
goto boot;
}
else
{
Fraction mFrac(a,b);
Fraction mFrac2(c,d);

mFrac + mFrac2;
mFrac * mFrac2;
mFrac - mFrac2;
mFrac > mFrac2;
mFrac < mFrac2;
mFrac = mFrac2;
mFrac == mFrac2;
mFrac != mFrac2;
mFrac2 <= mFrac2;
mFrac2 >= mFrac2;
}

getch();
return 0;
}

/* THE END */



0 blog comments below




FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.