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

(General math) a. Construct a class named Fractions containing two integer data

ID: 3590291 • Letter: #

Question

(General math) a. Construct a class named Fractions containing two integer data members named num and denom, used to store the numerator and denominator of a fraction having the form num/denom. Your class should include a default constructor that initializes num and denom to 1 if there's no user initialization, and it must prohibit a 0 denominator value. In addition, create member functions for displaying an object's data values and overloaded operator functions for adding, subtracting, multiplying, and dividing two Fraction objects, as follows: Addition: a/b + cid = (a * d + b *c) / (b * d) Subtraction: a/b-c/d = (a * d-b*c) / (b * d) Multiplication: a/b * cid = (a * c) / (b * d) Division: (a/b) / (C/d) = (a * d) / (b * c)

Explanation / Answer

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

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;

}

I have given error free code please get back to me if any queries regarding code

Thank you