In: Computer Science
afe Rational Fractions
In week 4 we completed Chapter 13 Programming Exercise #10 Page 974. Make sure you have a working fractionType class before starting this assignment. The template requirement from week 4 is not required for this assignment.
Your assignment this week is to make your fractionType class safe by using exception handling.
Use exceptions so that your program handles the exceptions division by zero and invalid input.
Use a custom Exception class called fractionException. The class must inherit from exception (see example in lecture and note the entire class can be implemented in the .h file as in the lecture).
Test your safe fractionType class with a main method that forces an invalid fraction and a divide by zero. The catch block in the main method must report which kind of error was encountered – i.e. invalid fraction or divide by zero.
The following can be used to test an exception:
try {
fractionType<int> num1(1,0);
fractionType<int> num2(0,3);
fractionType<int> num3;
cout << fixed;
cout << showpoint;
cout << setprecision(2);
num3 = num1 / num2;
cout << num1 << " / " << num2 << " = " << num3 << endl;
}
catch (fractionException e) {
cout << "Exception caight in main: " << e.what() << endl;
}
Submission Guidelines:
Submit modified fractionType class (.h and .cpp file if exists)
Submit fractionException class (.h file and .cpp file if exists)
Submit main test .cpp program to test the exception handling
/*fraction.h*/
#ifndef FRACTION_H
#define FRACTION_H
#include<iostream>
#include<exception>
using namespace std;
/*Exception class derived from std::exception*/
class fractionException : public exception
{
private:
const char *msg;
public:
fractionException(const char *m) :
msg(m){}
virtual const char *what() const
throw()
{
return
msg;
}
};
//fractionType class definition
class fractionType
{
private:
int n, d;
public:
fractionType() : n(0), d(1){}
fractionType(int num, int den) :
n(num), d(den)
{
if(den ==
0)
throw fractionException("Division by
zero");//Throw division by zero exception
}
fractionType operator/(const
fractionType &x);
friend ostream&
operator<<(ostream &out, const fractionType
&f);
friend istream&
operator>>(istream &in, fractionType &f);
};
#endif
/*End of franction.h*/
/*fraction.cpp*/
#include"fraction.h"
#include<iostream>
using namespace std;
//Function to perform division
fractionType fractionType::operator/(const fractionType
&x)
{
if(x.n == 0)
throw fractionException("Division
by zero");//Throw division by zero exception
fractionType f;
f.n = this->n*x.d;
f.d = this->d*x.n;
return f;
}
//Overloaded << operator
ostream& operator<<(ostream &out, const fractionType
&f)
{
out<<f.n<<"/"<<f.d;
return out;
}
//Overloaded >> operator
istream& operator>>(istream &in, fractionType
&f)
{
in>>f.n;
if(in.fail())
throw(fractionException("Invalid
Input"));//Throw invalid input exception
in.ignore(1);
in>>f.d;
if(in.fail())
throw(fractionException("Invalid
Input"));//Throw invalid input exception
if(f.d == 0)
throw fractionException("Division
by zero");//Throw division by zero exception
return in;
}
/*end of fraction.cpp*/
/*main.cpp*/
#include<iostream>
#include<iomanip>
#include "fraction.h"
using namespace std;
int main()
{
try
{
fractionType num1(1,2);
fractionType num2(0,3);
fractionType num3;
cout << fixed;
cout << showpoint;
cout <<
setprecision(2);
num3 = num1 / num2;
cout << num1 << " / "
<< num2 << " = " << num3 << endl;
}
catch (fractionException e)
{
cout << "Exception caught in
main: " << e.what() << endl;
}
return 0;
}
/*End of main.cpp*/
SAMPLE OUTPUT:
Feel free to reach out if you have any doubts or
require any modifications..
Please do rate the answer thanks...