(Rational Class) Create a class called Rational for performing arithmetic with r
ID: 3772862 • Letter: #
Question
(Rational Class) Create a class called Rational for performing arithmetic with rational (fractional) numbers. Write a program to test your class, with input from the user. Use integer variales to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it's decalred. Provide public functions which perform the following:
a. Add
b.Subtract
c.Multiply
d.Divide
e.Print
-any help with this exercise would be greatly appreciated
Code Template:
Explanation / Answer
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
public:
Rational( int = 0, int = 1 );
Rational addition( const Rational & ) const;
Rational subtraction( const Rational & ) const;
Rational multiplication( const Rational & ) const;
Rational division( const Rational & ) const;
void printRational () const;
private:
int numerator;
int denominator;
void reduce();
};
#endif
#include
#include "function.h"
using namespace std;
int main()
{
char s1;
int s2, s3, s4, s5;
Rational x;
while(cin >>s1>>s2>>s3>>s4>>s5)
{
if(cin.eof())
{
break;
}
Rational c(s2, s3), d(s4, s5);
if(s1 == '+')
{
x = c.addition( d );
x.printRational();
cout << ' ';
}
else if(s1 == '-')
{
x = c.subtraction( d );
x.printRational();
cout << ' ';
}
else if(s1 == '*')
{
x = c.multiplication( d );
x.printRational();
cout << ' ';
}
else if(s1 == '/')
{
x = c.division( d );
x.printRational();
cout << ' ';
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.