Using C++: Use exceptions so that your program handles the exceptions division by zero and invalid input. An example of invalid input would be entering a fraction such as 7 / 0 or 6 / a. An example of division by zero would be: 1 / 2 / 0 / 3.
Use a custom Exception class called fractionException. The class must inherit from exception. 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 {
fractionTypeint> num1(1,0);
fractionTypeint> num2(0,3);
fractionTypeint> num3;
cout fixed;
cout showpoint;
cout setprecision(2);
num3 = num1 / num2;
cout " / " " = " endl;
}
catch (fractionException e) {
cout "Exception caight in main: " e.what() endl;
}
MODIFY THE THREE PREEXISTING FILES [INCLUDED BELOW]:
fractionType.h = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = #ifndef FRACTION TYPE_H_INCLUDED #define FRACTION TYPE_H_INCLUDED #include
using namespace std; class fraction Type private: int num; int denom; public: fraction Type(); fraction Type(int n, int d); int gcd(int a, int b); fraction Type operator+(fraction Type r); fraction Type operator/(fraction Type r); fraction Type operator*(fraction Type r); fraction Type operator-(fraction Type r); bool operator>=(fraction Type r); bool operator(fraction Type r); //Extraction friend ostream &operator>(istream &, fraction Type &); #endif // FRACTION TYPE_H_INCLUDED = = = ===================== ======================= fractionType.cpp #include #include "fraction Type.h" using namespace std; int fraction Type::gcd(int a, int b) return (b == 0 ? a: gcd(b, a%b)); fraction Type fraction Type() num = 1; denom = 1; l/Constructor fraction Type fraction Type(int n, int d) int, g l/if denominator is equal to 0 if(d == 0) cout (fraction Type r) return ((num *r.denom) > (r.num * denom)); //Overloading more than operator bool fraction Type::operator=(fraction Type r) return ((num *r.denom) >= (r.num * denom)); ostream &operator>(istream &input, fraction Type &D) { char ch; input >> D.num >> ch >> D.denom; return input; ======================== ==================== main.cpp #include #include "fraction Type.h" using namespace std; int main() fraction Type f1, f2, f3; cout > f1; cout > f2; f3 = f1 + f2; cout using namespace std; class fraction Type private: int num; int denom; public: fraction Type(); fraction Type(int n, int d); int gcd(int a, int b); fraction Type operator+(fraction Type r); fraction Type operator/(fraction Type r); fraction Type operator*(fraction Type r); fraction Type operator-(fraction Type r); bool operator>=(fraction Type r); bool operator(fraction Type r); //Extraction friend ostream &operator>(istream &, fraction Type &); #endif // FRACTION TYPE_H_INCLUDED = = = ===================== ======================= fractionType.cpp #include #include "fraction Type.h" using namespace std; int fraction Type::gcd(int a, int b) return (b == 0 ? a: gcd(b, a%b)); fraction Type fraction Type() num = 1; denom = 1; l/Constructor fraction Type fraction Type(int n, int d) int, g l/if denominator is equal to 0 if(d == 0) cout (fraction Type r) return ((num *r.denom) > (r.num * denom)); //Overloading more than operator bool fraction Type::operator=(fraction Type r) return ((num *r.denom) >= (r.num * denom)); ostream &operator>(istream &input, fraction Type &D) { char ch; input >> D.num >> ch >> D.denom; return input; ======================== ==================== main.cpp #include #include "fraction Type.h" using namespace std; int main() fraction Type f1, f2, f3; cout > f1; cout > f2; f3 = f1 + f2; cout