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

Given the following definition of class Fraction C OR C++ #ifndef FRACTION_HPP #

ID: 3850626 • Letter: G

Question

Given the following definition of class Fraction

C OR C++

#ifndef FRACTION_HPP

#define FRACTION_HPP

#include <iostream>

#include <sstream>

#include <cstdlib>

#include <string>

#include <cmath>

using namespace std;

namespace homeworks {

    class Fraction {

    public:

        Fraction();

        Fraction(int n, int d);

        Fraction(string s);

        Fraction(const Fraction& orig);

        ~Fraction();

        Fraction operator+(const Fraction& rhs);

        Fraction operator-(const Fraction& rhs);

        Fraction operator*(const Fraction& rhs);

        Fraction operator/(const Fraction& rhs);

          Fraction& operator=(const Fraction& rhs);

         

          bool operator==(const Fraction& rhs);

          bool operator!=(const Fraction& rhs);

          bool operator<(const Fraction& rhs);

          bool operator>(const Fraction& rhs);

        friend ostream& operator<<(ostream &strm, const Fraction &a);

        friend istream& operator>>(istream &strm, Fraction &a);

    private:

        int numerator, denominator;

        int gcd(int a, int b);

        void normalize();

    };

}

#endif /* FRACTION_HPP */

A. Implement the constructor Fraction(string s);

Example: Fraction f(“1/2”);

B. Implement the Operator+

Example: Fraction f1(1,2);

Fraction f2(1,2);

Fraction f3 = f1 + f2;

C. Implement the Operator ==

Example: Fraction f1(1,2);

        Fraction f2(1,2);

        if ( f1 == f2) cout << “Iguales” << endl;

D. Write a test case for the constructor Fraction(string s);

  

"1/2" == 1/2

Test 1 passed

E. Write a test case for the Operator+

1/2 + 1/2 = 1

1/2 + 1/4 = 3/4

Test 2 passed

Explanation / Answer

// Please note fraction.h is changed with less number of functions as required by question.

// fraction.h

#ifndef FRACTION_HPP
#define FRACTION_HPP

#include <iostream>
#include <sstream>
#include <cstdlib>
#include <string>
#include <cmath>

using namespace std;
namespace homeworks {
class Fraction {
public:
Fraction();
Fraction(int n, int d);
Fraction(string s);
Fraction(const Fraction& orig);
~Fraction();

Fraction operator+(const Fraction& rhs);

bool operator==(const Fraction& rhs);
int getNumerator();
int getDenominator();
  
private:
int numerator, denominator;
int gcd(int a, int b);
void normalize();
};
}
#endif /* FRACTION_HPP */

// fraction.cpp

#include "fraction.h"
using namespace homeworks;
Fraction::Fraction()
{
  
}

Fraction::Fraction(int n, int d)
{
numerator = n;
denominator = d;
normalize();
}

Fraction::Fraction(string s)
{
std::istringstream ss(s);
char c;
ss >> numerator >> c >> denominator;
normalize();
}

Fraction::Fraction(const Fraction& orig)
{
numerator = orig.numerator;
denominator =orig.denominator;
}
  
Fraction::~Fraction()
{
}

Fraction Fraction::operator+(const Fraction& rhs)
{
int n = numerator*(rhs.denominator) + denominator*(rhs.numerator);
int d= denominator * (rhs.denominator);
Fraction f = Fraction(n, d);
return f;
}

int Fraction::getNumerator() {return numerator;}
int Fraction::getDenominator() {return denominator;}
bool Fraction::operator==(const Fraction& rhs)
{
return (numerator == rhs.numerator) && (denominator == rhs.denominator);
}
int Fraction::gcd(int a, int b){return b==0 ? a : gcd(b,a%b);}
void Fraction::normalize()
{
int g= gcd(numerator, denominator);
numerator = numerator/g;
denominator= denominator/g;
}

// main.cpp

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

int main()
{
homeworks::Fraction f("1/2");
homeworks::Fraction f1(1,2);
homeworks::Fraction f2(1,2);
if ( f1 == f2) cout << "Iguales" << endl;
if ( f == f1) cout << "Test 1 passed" << endl;
  
homeworks::Fraction f3 = f1 + f2;
homeworks::Fraction f4(1,1);
homeworks::Fraction f5(1,4);
homeworks::Fraction f6 = f1+f5;
homeworks::Fraction f6Result(3,4);
if ( (f3 == f4) && (f6 == f6Result) ) cout << "Test 2 passed" << endl;
  
return 0;
}

// sample run

Iguales                                                                                                                                                       

Test 1 passed                                                                                                                                                 

Test 2 passed   

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