Okay, so my assignment is to overload the following operators and use them in main:
+, -, *, /, ==, !=, >, <, <=, >=
and these friend functions:
>>, <<
Now, for the assignment our instructor gave us a program with a completed header file, main, .cpp, etc for us to just insert the overloaded operators.
I am having problems associating the logic with the operators when declaring the functions in the implementation file (.cpp).
Here's the code, the green is what I've done so far and the rest is my instructors code:
HEADER
// Class Fraction Header File
#ifndef FRAC
#define FRAC
class Fraction
{
private:
int numerator;
int denominator;
int greatestComDiv (int n1, int n2);
public:
Fraction (int numer = 0, int denom = 1);
Fraction (const Fraction& copyFrom);
void store (int numer, int denom);
void print () const;
const Fraction& operator++ (); // Prefix ++
const Fraction& operator++ (int); // Postfix ++
Fraction operator+ (const Fraction&);
Fraction operator- (const Fraction&);
Fraction operator* (const Fraction&);
Fraction operator/ (const Fraction&);
bool operator== (const Fraction&);
bool operator!= (const Fraction&);
Fraction operator> (const Fraction&);
Fraction operator< (const Fraction&);
Fraction operator<= (const Fraction&);
Fraction operator>= (const Fraction&);
friend const Fraction
operator>> (const Fraction&, const Fraction&);
friend const Fraction
operator<< (const Fraction&, const Fraction&);
Fraction& operator= (const Fraction& fr);
void operator+= (const Fraction& fr2);
operator float () const;
};
#endif
IMPLEMENTATION:
// Class Fraction Implementation File
#include <cstdlib>
#include <iostream>
using namespace std;
#include "fraction.h"
// ================ Fraction :: Fraction =================
// Default Constructor:
// Initializes fraction to values in parameter list
// ensuring that the fraction is normalized.
// Pre numen and denom contain fraction values
// Post fraction object initialized
// Return nothing
Fraction :: Fraction (int numer, int denom)
{
if (denom == 0)
{
cout << "Error: zero denominator changed to 1" << endl;
denom = 1;
}
if (denom < 0)
{ // Ensure that any negative is in numerator
denom = -denom;
numer = -numer;
}
int gcd = greatestComDiv (abs(numer), abs(denom));
numer = numer / gcd;
denom = denom / gcd;
numerator = numer;
denominator = denom;
}
// ================ Fraction :: Fraction =================
// Copy constructor
// Pre copyFrom exists and has values to be copied
// Post new object created and data copied
// Return nothing
Fraction :: Fraction (const Fraction& copyFrom)
{
numerator = copyFrom.numerator;
denominator = copyFrom.denominator;
}
// ================== Fraction :: store ================
// Store the numerator and denominator in the fraction
// class. Calls constructor to ensure normalization.
// Pre numer and denom contain the numerator
// and denominator respectively
// Post data stored
// Return nothing
void Fraction :: store (int numer, int denom)
{
*this = Fraction (numer, denom);
return;
}
// ================== Fraction :: print ================
// Prints the numerator and denominator as a fraction.
// Pre fraction class must contain data
// Post data printed
// Return nothing
void Fraction :: print () const
{
cout << numerator << "/" << denominator;
return;
}
// ====================== operator+ ========================
// Add input fraction to host fraction.
// Pre valid input fraction
// Post new fraction normalized
// Return new, normalized Fraction object
Fraction Fraction :: operator+ (const Fraction& fr2)
{
int numen = (numerator * fr2.denominator)
+ (fr2.numerator * denominator);
int denom = denominator * fr2.denominator;
return Fraction (numen, denom);
}
// ================== ++operator ==================
// Add one to the fraction.
// Pre nothing
// Post One added
//
const Fraction& Fraction :: operator++ ()
{
numerator += denominator;
return (*this);
}
// ================== operator++ ==================
// Add one to the fraction using postfix design.
// Pre nothing
// Post One added
//
const Fraction& Fraction :: operator++ (int)
{
// Save value for return
Fraction saveObject = *this; // Call copy constr
// const Fraction saveObject(*this); // Call copy constr
numerator += denominator;
return saveObject;
}
// ================== operator= ==================
// Overload assignment operator for fraction class
// Pre Nothing
// Post Logical copy of fraction
//
Fraction& Fraction :: operator= (const Fraction& fr)
{
numerator = fr.numerator;
denominator = fr.denominator;
return *this;
}
// ================== Fraction :: operator float ==================
// Convert fraction to float type.
// Pre nothing
// Post numerator / denominator returned as floating point value
// Return floating point value of num / denom
Fraction :: operator float () const
{
return (static_cast<float>(numerator) / denominator);
}
// ================== Fraction :: operator+= ==================
// Add two fractions.
// Pre fractions contain values.
// Post sum stored in calling fraction
// Return nothing
void Fraction :: operator+= (const Fraction& fr2)
{
numerator =
(numerator * fr2.denominator)
+ (fr2.numerator * denominator);
denominator *= fr2.denominator;
*this = Fraction (numerator, denominator);
return;
}
// ================== greatestComDiv ==================
// Determine the greatest common divisor of two numbers.
// Pre Given two integers
// Post GCD calculated
// Return GCD as integer
int Fraction:: greatestComDiv (int n1, int n2)
{
if (n2 == 0) // Base case
return n1;
// General Case
return greatestComDiv (n2, n1 % n2);
}
// ================== Fraction :: operator-==================
Fraction Fraction :: operator- (const Fraction& fr2)
{
int numen = (numerator * fr2.denominator)
- (fr2.numerator * denominator);
int denom = denominator * fr2.denominator;
return Fraction (numen, denom);
}
// ================== Fraction :: operator*==================
Fraction Fraction :: operator* (const Fraction& fr2)
{
int numen = (numerator * fr2.denominator);
int denom = (denominator * fr2.denominator);
return Fraction (numen, denom); // Trouble here
}
MAIN:
// Demonstrate Overloaded Operators
#include <cstdlib>
#include <iostream>
using namespace std;
#include "fraction.h"
int main(int argc, char *argv[])
{
cout << "\n\tBegin Overloaded Operator Demo\n\n";
Fraction fr1 (1, 5);
cout << "\nFraction fr1: ";
fr1.print();
Fraction fr2 (3, 4);
cout << "\nFraction fr2: ";
fr2.print();
cout << endl;
fr2 = fr2 + fr1;
cout << "\nFraction fr2 when added to fr1: ";
fr2.print();
cout << endl;
fr2 = fr2 - fr1;
cout << "\nFraction fr2 when subtracted from fr1: ";
fr2.print();
cout << endl;
fr2 = fr2 * fr1;
cout << "\nFraction fr2 when multiplied by fr1: ";
fr2.print();
cout << endl;
Fraction fr3 = fr1++;
cout << "\nFraction fr3: ";
fr3.print();
cout << "\nFraction fr1: ";
fr1.print();
cout << endl;
++fr2;
cout << "\nFraction ++fr2: ";
fr2.print();
double val = fr2;
cout << "\n\nfloat value: " << val << endl;
cout << endl;
cout << "\n\tEnd Overloaded Operator Demo\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
As you can see I'm not sure how to fill the remaining functions. The '*' operator is what I was working on last (I know I'm still very early into the assignment), but I can't wrap my head around how to word the logic of "numerator 1 times numerator 2 and denominator 1 times denominator 2" in code. It's especially confusing because I'm forced to jump into foreign code (code that I didn't write).
Thank you!