In: Computer Science
Programming II: C++ - Programming Assignment
Fraction Object with Operator Overloads
Overview
In this assignment, the student will write a C++ program that implements a “fraction” object. When writing the object, the student will demonstrate mastery of implementing overloaded operators in a meaningful way for the object.
When completing this assignment, the student should demonstrate mastery of the following concepts:
· Mathematical Modeling - Fractions
· Operator Overloading – Binary Operators (Internal Overload)
· Operator Overloading – Binary Operator (External Overload)
Assignment
Write a C++ object that implements a “fraction” object. In more precise terms, your object will contain the data needed to represent a rational expression. In mathematics, it is understood that all of the real numbers can be defined as the quotient of two integer values. Your fraction object should contain attributes that represent these two integers. When implementing your fraction object, you should minimally include the following components:
· You should provide a constructor method that initialized the rational expression to (0/1).
· You should provide a constructor method that allows client code to manually set integer values for the numerator and denominator.
· Your object should restrict mutation of itself so only valid rational expressions exist within the object (i.e. a value of 4/0 should be rejected).
· Your object should contain a series of getter and setter methods that allow safe mutation of the numerator and denominator.
· Your getter and setter methods must contain meaningful interfaces that communicate whether values were effectively modified by client code requests through return values. The driver code you write to demonstrate the object should also demonstrate these interfaces.
· Your object should contain overloaded binary arithmetic operators for addition, subtraction, multiplication, and division.
· Your object should always reduce the value represented by the contained rational expression to its lowest terms.
· Your object should contain a binary external operator overload for the stream insertion (<<) operator.
· Your driver code should instantiate a few instances of fractions and demonstrate all the arithmetic operations and displaying the object’s information on the console with overloaded behavior when interacting
with cout (i.e. Your driver should have a statement resembling: cout << myFractionObject;
Fraction.h
#ifndef Fraction_H
#define Fraction_H
#include<iostream>
using namespace std;
class Fraction{
private:
int numerator;
int denominator;
int GCD(int, int);
public:
Fraction();
Fraction(int,int);
void setNumerator(int);
void setDenominator(int);
int getNumerator();
int getDenominator();
Fraction operator +(Fraction);
Fraction operator -(Fraction);
Fraction operator *(Fraction);
Fraction operator /(Fraction);
friend ostream & operator << (ostream &, const Fraction&);
};
#include"Fraction.cpp"
#endif
Fraction.cpp
#include"Fraction.h"
using namespace std;
// private member function to calculate
// gcd, to help reducing the Fraction to
// simplest term
int Fraction::GCD(int n, int d)
{
int result;
if(n % d == 0)
result = d;
else
result = GCD(d, n%d);
return result;
}
// defualt constructor
Fraction::Fraction()
{
numerator = 0;
denominator = 1;
}
// parameterized constructor
// to initialize the fraction with user defined
// numerator and denominator
Fraction::Fraction(int n, int d)
{
// checking whether denominator is not 0
if(d != 0)
{
// reducing the numbers
int gcd = GCD(n, d);
setNumerator(n/gcd);
setDenominator(d/gcd);
}
// else make the fraction as 0
else
{
numerator = 0;
denominator = 1;
}
}
// setting the numerator
void Fraction::setNumerator(int n)
{
numerator = n;
}
// setting the denominator
void Fraction::setDenominator(int d)
{
denominator = d;
}
// returning the numerator
int Fraction::getNumerator()
{
return numerator;
}
// returning the denominator
int Fraction::getDenominator()
{
return denominator;
}
// overloaded addition operator
Fraction Fraction::operator +(Fraction f)
{
int newNumer = (numerator * f.getDenominator() + denominator*f.getNumerator());
int newDenom = (denominator * f.getDenominator());
Fraction result(newNumer, newDenom);
return result;
}
// overloaded subtraction operator
Fraction Fraction::operator -(Fraction f)
{
int newNumer = (numerator * f.getDenominator() - denominator*f.getNumerator());
int newDenom = (denominator * f.getDenominator());
Fraction result(newNumer, newDenom);
return result;
}
// overloaded multiplication operator
Fraction Fraction::operator *(Fraction f)
{
int newNumer = numerator*f.getNumerator();
int newDenom = denominator*f.getDenominator();
Fraction result(newNumer, newDenom);
return result;
}
// overloaded division operator
Fraction Fraction::operator /(Fraction f)
{
int newNumer = (numerator*f.getDenominator());
int newDenom = (denominator*f.getNumerator());
Fraction result(newNumer, newDenom);
return result;
}
// overloaded extraction operator for cout
ostream & operator <<(ostream &out, const Fraction &f)
{
Fraction newF = f;
if(f.numerator < 0 && f.denominator > 0)
{
newF.setNumerator(newF.numerator*-1);
out << "-" << newF.numerator << "/" << newF.denominator;
}
else if(f.numerator > 0 && f.denominator < 0)
{
newF.setDenominator(newF.denominator * -1);
out << "-" << newF.numerator << "/" << newF.denominator;
}
else
{
out << f.numerator << "/" << f.denominator;
}
return out;
}
Driver.cpp
#include"Fraction.h"
int main()
{
// creating fraction objects
Fraction myFractionObject1(2, 7);
Fraction myFractionObject2(3, 7);
// performing the binary operations
Fraction addedObjects = myFractionObject1 + myFractionObject2;
Fraction subtractedObjects = myFractionObject1 - myFractionObject2;
Fraction multipliedObjects = myFractionObject1 * myFractionObject2;
Fraction deividedObjects = myFractionObject1 / myFractionObject2;
// printing the results
cout << "myFractionObject1 = " << myFractionObject1 << endl;
cout << "myFractionObject2 = " << myFractionObject2 << endl << endl;
cout << "Sum of " << myFractionObject1 << " + " << myFractionObject2
<< " = " << addedObjects << endl;
cout << "Difference of " << myFractionObject1 << " - " << myFractionObject2
<< " = " << subtractedObjects << endl;
cout << "Product of " << myFractionObject1 << " * " << myFractionObject2
<< " = " << multipliedObjects << endl;
cout << "Division of " << myFractionObject1 << " / " << myFractionObject2
<< " = " << deividedObjects << endl;
}