Question
Need Help! Write the implementation for this file (Rational.h) that deals with rational numbers; class Rational { public: Rational(); // constructs 0/1 Rational(int num_, int
Need Help!
Write the implementation for this file (Rational.h) that deals with rational numbers;
class Rational { public: Rational(); // constructs 0/1 Rational(int num_, int denom_); // exits prog. if denom_ equals 0
int getNum() const; // getters int getDenom() const; void setNum(int num_); // setters void setDenom(int denom,_); // exits prog. if denom_ equals 0 Rational norm() const; // returns normalized number(eg: 8/-9 normalized is -8/9) Rational reduce() const; // reduces number to lowest terms int cmp(Rational &r) const; // returns (-1 if r1 < r2), (0 if r1 = r2), (1 if r1 > r2)
Rational add(Rational& op2) const; // adds 2 numbers Rational sub(Rational& op2) const; // subtracts op2 from op1 friend std::ostream& operator <<(std::ostream& os, Rat rat); private: int num,denom; // numerator, denominator int gcd(int a, int b) const; // greatest common divisor int lcm(int x, int y) const; // least common multiple };
Use this driver.
#include
int main() { Rational r1(3,4), r2(9,-16), r3(1,-4); cout << r1 << endl; cout << r2 << endl; cout << r3 << endl; cout << r2.norm() << endl; cout << r1.cmp(r2) << endl; }
This is the exact output:
3/4
9/-16
1/-4
-9/16
1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started