C++ (RationalNumber Class) Create a class RationalNumber (fractions) with the fo
ID: 3542840 • Letter: C
Question
C++
(RationalNumber Class) Create a class RationalNumber (fractions) with the following capabilities:
A) Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions that are not in reduced form and avoids negative denominators
B) Overload the addition, subtraction, multiplication and division operators for this class.
C) Overload the relational and equality operators for this class.
IMPORTANT: I have already searched for the solution to this program. DO NOT COPY AND PASTE SOMEONE ELSES ANSWER. I have already seen the other programs on chegg for this. The ones that I have found are close to 300 lines of code and I just think it can be simplified. (I could be wrong)
I need good notes so I can piece together the puzzle and learn it. I do NOT want someone to just do it for me. I'm actually trying to learn this.
This is a beginner programming course so I would prefer this program to be as simple as possible.
Whoever provides me with what I have requested will receive a 5 star rating. Thanks so much in advance
Explanation / Answer
//============================================================================
// Name : chegg_practice.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <stdlib.h>
using namespace std;
class Rational {
private:
int numerator;
int denominator;
public:
//constructor when no initializers are provided
//Default values taken as 1 for both
Rational() {
numerator = 1;
denominator = 1;
}
//constructor when initializers are provided
//first for the numerator and second for the denominator
Rational(int x, int y) {
numerator = x;
denominator = y;
if (y == 0) {
cout << "denominator can't be 0" << endl;
exit(EXIT_FAILURE);
}
//calculating GCD
while (y != 0) {
int r = x % y;
x = y;
y = r;
}
// Expressing in reduced form
numerator = numerator / x;
denominator = denominator / x;
}
//addition function PART A
Rational add(Rational X, Rational Y) {
int x = X.numerator * Y.denominator + Y.numerator * X.denominator;
int y = X.denominator * Y.denominator;
Rational result(x, y);
return result;
}
//subtraction function PART B
Rational subtract(Rational X, Rational Y) {
int x = X.numerator * Y.denominator - Y.numerator * X.denominator;
int y = X.denominator * Y.denominator;
Rational result(x, y);
return result;
}
//multiply function PART C
Rational multiply(Rational X, Rational Y) {
int x = X.numerator * Y.numerator;
int y = X.denominator * Y.denominator;
Rational result(x, y);
return result;
}
//divide function PART D
Rational divide(Rational X, Rational Y) {
int x = X.numerator * Y.denominator;
int y = X.denominator * Y.numerator;
Rational result(x, y);
return result;
}
bool Relational(Rational X, Rational Y, char op) {
Rational sub = subtract(X, Y);
if (op=='<' && sub.numerator < 0)
return true;
if (op=='>' && sub.numerator > 0)
return true;
return false;
}
bool equality(Rational X, Rational Y) {
Rational sub = subtract(X, Y);
if (sub.numerator == 0)
return true;
else
return false;
}
void printRational(Rational X) {
cout << X.numerator << "/" << X.denominator << endl;
}
void printFloat(Rational X) {
cout << X.numerator * 1.0 / X.denominator << endl;
}
};
int main() {
Rational number1(2, 4);
number1.printRational(number1); //output = 1/2
Rational number2(5, 1);
//you may use any object to call the public functions
number1.printRational(number1.add(number1, number2)); //output = 11/2
number1.printRational(number2.multiply(number1, number1)); //output = 1/4
cout<<number1.Relational(number1,number2,'<')<<endl; //output = 0 (because its false)
cout<<number1.equality(number1,number1)<<endl; //output = 1 (because its true)
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.