Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Create a class named Fraction containing two integer data members named

ID: 3763478 • Letter: P

Question

Problem

Create a class named Fraction containing two integer data members namednum and denom used to store the numerator and denominator of a fraction having the form num/denom.

I need help with my implementation section. namely I need help with the operator>> and operator<< function defintions since I'm not really sure how to set them up properly. Also in my Int main I don't know if I wrote proper code to display the correct results. Help would be appreciated.

******************************************

Specification

Include default and parametrized constructors.

The default constructor should initialize num and denom to 1.

Neither constructor should allow a 0 denominator value.

Create a member function to display an object's data members.

Overload operator functions for

addition +: a/b + c/d = (a * d + b * c) / (b * d)

subtraction -: a/b - c/d = (a * d - b * c) / (b * d)

multiplication *: a/b * c/d = (a * c) / (b * d)

division /: a/b / c/d = (a * d) / (b * c)

stream insertion: << to send a fraction to the screen.

stream extraction: >> to input a fraction from the keyboard.

Include the class in a C++ program that tests each member function.

Header File

-----------------

#ifndef FRACTION_H
#define FRACTION_H


class Fraction
{
public:
Fraction(int n = 0, int d = 1);
int getNum() const;

Fraction operator+(const Fraction& f2) const;
Fraction operator-(const Fraction& f2) const;
Fraction operator*(const Fraction& f2) const;
Fraction operator/(const Fraction& f2) const;
Fraction operator>>(const Fraction& f2) const;
Fraction operator<<(const Fraction& f2) const;


private:
int Num, Denom;
};

#endif // FRACTION_H

Implementation File
-------------------------------

#include "fraction.h"
#include <iostream>
using namespace std;

Fraction::Fraction(int n, int d)
{
Num = n;
Denom = d;

if( d == 0 )
cout << "Your not Chuck Norris!";
}

int Fraction::getNum() const
{
return Num;
}

Fraction Fraction::operator+(const Fraction& f2) const
{
Fraction total;

total.Num = Num * f2.Denom + f2.Num * Denom;
total.Denom = Denom * f2.Denom;


return total;

}

Fraction Fraction::operator-(const Fraction& f2) const
{
Fraction totalDiff;

totalDiff.Num = Num * f2.Denom + f2.Num * Denom;
totalDiff.Denom = Denom * f2.Denom;

return totalDiff;
}

Fraction Fraction::operator*(const Fraction& f2) const
{
Fraction totalMult;

totalMult.Num = Num * f2.Num;
totalMult.Denom = Denom * f2.Denom;

return totalMult;
}

Fraction Fraction::operator/(const Fraction& f2) const
{
Fraction totalDiv;

totalDiv.Num = Num *f2.Denom;
totalDiv.Denom = Denom * f2.Num;

return totalDiv;
}

Fraction Fraction::operator>>(const Fraction& f2) const
{
Fraction
  
  
  
}

Fraction Fraction::operator<<(const Fraction& f2) const
{
  
  
  
}

Int main

-----------------

#include <iostream>
#include "Fraction.h"

using namespace std;


int main()
{
Fraction f1(1,2);
Fraction f2(2,3);

cout << "This is my numerator " << f1.getNum() << endl;
cout << "Adding f1 and f2, my numerator should be 7!" << (f1 + f2).getNum() << endl;
cout << "Subtracting f1 and f2 my numerator should be " << (f1 - f2).getNum() << endl;
cout << "Multiplying f1 and f2 my numerator should be " << (f1 * f2).getNum() << endl;
cout << "Dividing f1 and f2 my numerator should be " << (f1 / f2).getNum() << endl;


return 0;
}

Explanation / Answer

Program code:

header file:

#include"stdafx.h"

#ifndef FRACTION_H

#define FRACTION_H

class Fraction

{

public:

Fraction(int n = 0, int d = 1);

int getNum() const;

Fraction operator+(const Fraction& f2) const;

Fraction operator-(const Fraction& f2) const;

Fraction operator*(const Fraction& f2) const;

Fraction operator/(const Fraction& f2) const;

Fraction operator>>(const Fraction& f2) const;

Fraction operator<<(const Fraction& f2) const;

private:

int Num, Denom;

};

#endif

Implementation of operator overloading:

#include "stdafx.h"

#include <iostream>

#include "Fraction1.h"

using namespace std;

Fraction::Fraction(int n, int d)

{

Num = n;

Denom = d;

if( d == 0 )

cout << "Your not Chuck Norris!";

}

int Fraction::getNum() const

{

return Num;

}

Fraction Fraction::operator+(const Fraction& f2) const

{

Fraction total;

total.Num = Num * f2.Denom + f2.Num * Denom;

total.Denom = Denom * f2.Denom;

return total;

}

Fraction Fraction::operator-(const Fraction& f2) const

{

Fraction totalDiff;

totalDiff.Num = Num * f2.Denom - f2.Num * Denom;

totalDiff.Denom = Denom * f2.Denom;

return totalDiff;

}

Fraction Fraction::operator*(const Fraction& f2) const

{

Fraction totalMult;

totalMult.Num = Num * f2.Num;

totalMult.Denom = Denom * f2.Denom;

return totalMult;

}

Fraction Fraction::operator/(const Fraction& f2) const

{

Fraction totalDiv;

totalDiv.Num = Num *f2.Denom;

totalDiv.Denom = Denom * f2.Num;

return totalDiv;

}

Fraction Fraction::operator>>(const Fraction& f2) const

{

return f2.getNum() << '>>' << f2.Denom;

}

Fraction Fraction::operator<<(const Fraction& f2) const

{

return f2.getNum() << '<<' << f2.Denom;

}

int main()

{

Fraction f1(1,2);

Fraction f2(2,3);

cout << "This is my numerator " << f1.getNum() << endl;

cout << "Adding f1 and f2, my numerator should be" << (f1 + f2).getNum() << endl;

cout << "Subtracting f1 and f2 my numerator should be " << (f1 - f2).getNum() << endl;

cout << "Multiplying f1 and f2 my numerator should be " << (f1 * f2).getNum() << endl;

cout << "Dividing f1 and f2 my numerator should be " << (f1 / f2).getNum() << endl;

cout << "shifting f1 and f2 my numerator should be " << (f1 << f2).getNum() << endl;

cout << "shifting f1 and f2 my numerator should be " << (f1 >> f2).getNum() << endl;

system("pause");

return 0;

}

Sample output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote