Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What are the possible downsides to management by exception?

Answered: 1 week ago

Question

Find the derivative. f(x) 8 3 4 mix X O 4 x32 4 x32 3 -4x - x2

Answered: 1 week ago