Question: Done in C++ (using Dev-C++). Create a class for rational numbers using the following class prototype. class Rational { int numer, denom; public: Rational(); Rational(int

Done in C++ (using Dev-C++).

Create a class for rational numbers using the following class prototype.

class Rational {

int numer, denom;

public:

Rational();

Rational(int num, int den);

Rational operator+(Rational r);

Rational operator-(Rational r);

Rational operator*(Rational r);

Rational operator/(Rational r);

Rational operator+(int x);

Rational operator-(int x);

Rational operator*(int x);

Rational operator/(int x);

int getNumer();

int getDenom();

void print(); }; Test the class implementation with the following code. #include "Rational.h"

#include

using namespace std; int main() {

Rational a(1,2), b(5,2), c(1,4), d(5,3), e(3,2),f;

f = (a+1) + b*(c-2)*3 - d/e/2 ;

f.print();

return 0;

}

THE CODE I HAVE WRITTEN FOR THIS IS SHOWN BELOW. I am getting an issue with my operator functions that are trying to accept integers [i.e. Rational Rational::operator(int x)] The error message says "[Error] 'declaration of "Rational Rational::operator-(int x)' outside of class is not defintion [-fpermissive]. This is happeneing for all functions taking int x. How do I fix this issue?? I assume it has something to do with the fact that the class does not know what to do with int values, but I am unsure how to fix that.

#include

#include

using namespace std;

class Rational { int numer, denom; public: Rational(); Rational(int num, int den); void setVals(int num, int den); Rational operator+(Rational r); Rational operator-(Rational r); Rational operator*(Rational r); Rational operator/(Rational r); Rational operator+(int x); Rational operator-(int x); Rational operator*(int x); Rational operator/(int x); int getNumer(); int getDenom(); void print(); };

Rational::Rational() { }

Rational::Rational(int num, int den) { numer = num; denom = den; }

Rational Rational::operator+(Rational r) { Rational a; // addition a.numer = (numer*r.denom + r.numer*denom); a.denom = (denom*r.denom); return a; } Rational Rational::operator-(Rational r) { Rational a; a.numer = (numer*r.denom - r.numer*denom); a.denom = (denom*r.denom); return a; } Rational Rational::operator*(Rational r) { Rational a; // addition a.numer = (numer*r.numer); a.denom = (denom*r.denom); return a; } Rational Rational::operator/(Rational r) { Rational a; a.numer = (numer*r.denom); a.denom = (r.numer*denom); return a; } Rational Rational::operator+(int x) { Rational a; // addition a.numer = (numer*1 + x*denom); a.denom = (denom*1); return a; } Rational Rational::operator-(int x); { Rational a; a.numer = (numer*1 - x*denom); a.denom = (denom*1); return a; } Rational Rational::operator*(int x); { Rational a; // addition a.numer = (numer*x); a.denom = (denom*1); return a; } Rational Rational::operator/(int x); { Rational a; a.numer = (numer*1); a.denom = (x*denom); return a; }

int getNumer() { return numer; } int getDenom() { return denom; } void print() { cout << "The value is: " << numer << "/" << denom << endl; }

int main() { Rational a(1,2), b(5,2), c(1,4), d(5,3), e(3,2), f; f = (a+1) + b*(c-2)*3 - d/e/2; f.print(); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!