Question

In: Computer Science

C++ make a rational class that includes these members for rational -private member variables to hold...

C++ make a rational class that includes these members for rational
-private member variables to hold the numerator and denominator values
-a default constructor
-an overloaded constructor that accepts 2 values for an initial fraction
-member fractions add(), sub(), mul(), div(), less(), eq(), and neq() (less should not return true if the object is less than argument)
-a member function that accepts an argument of type of ostream that writes the fraction to that open output stream

do not let numerator or denominator value be negative

Solutions

Expert Solution

//C++ Code

#include<iostream>
using namespace std;

class Rational {
private:
   int num, den;
   void simplify()
   {
       //reduced to simplest form
       for (int i = this->den * this->num; i > 1; i--) {
           if ((this->den % i == 0) && (this->num % i == 0)) {
               this->den /= i;
               this->num /= i;
           }

       }
      

   }
public:
   //defaulat constructor
   Rational();
   //overlaod constructor
   Rational(int n, int d);
   //member functions
   Rational& add(const Rational other);
   Rational& mul(const Rational other);
   Rational& div(const Rational other);
   Rational& sub(const Rational other);
   bool less(const Rational other);
   bool eq(const Rational other);
   bool neq(const Rational other);

   //overload ostream
   friend ostream& operator<<(ostream& out, const Rational& other);

};

//implementation

//defaulat constructor
Rational::Rational()
{
   num = 0;
   den = 1;
}
//overlaod constructor
Rational::Rational(int n, int d)
{
   if (d < 0)
   {
       d = d * -1;
       n = n * -1;
      
   }
   num = n;
   den = d;
   simplify();
}
//member functions
Rational& Rational::add(const Rational other)
{
   Rational temp;
   temp.num = num * other.den + other.num * den;
   temp.den = den * other.den;
   temp.simplify(); //calling the helper function to reduce the fraction
   return temp;
}
Rational& Rational::mul(const Rational other)
{
   Rational temp; //creating a temporary object
   temp.num = num * other.num;
   temp.den = den * other.den;
   temp.simplify(); //calling the helper function to reduce the fraction
   return temp;
}
Rational& Rational::div(const Rational other)
{
   Rational temp; //creating a temporary object
   temp.num = num * other.den;
   temp.den = den * other.num;
   temp.simplify(); //calling the helper function to reduce the fraction
   return temp;
}
Rational& Rational::sub(const Rational other)
{
   Rational temp;
   temp.num = num * other.den - other.num * den;
   temp.den = den * other.den;
   temp.simplify(); //calling the helper function to reduce the fraction
   return temp;
}
bool Rational::less(const Rational other)
{
   return num * other.den < other.num * den;
}
bool Rational::eq(const Rational other)
{
   return num * other.den == other.num * den;
}
bool Rational::neq(const Rational other)
{
   return num * other.den != other.num * den;
}

//overload ostream
ostream& operator<<(ostream& out, const Rational& other)
{
   out << other.num << "/" << other.den;
   return out;
}

//main()
int main()
{
   //Create objects
   Rational r1(3, 4), r2(7, 5);
   Rational addR = r1.add(r2);
   cout << r1 << " + " << r2 << " = " << addR << endl;
   Rational subR = r1.sub(r2);
   cout << r1 << " - " << r2 << " = " << subR << endl;

   Rational mulR = r1.mul(r2);
   cout << r1 << " * " << r2 << " = " << mulR << endl;

   Rational divR = r1.div(r2);
   cout << r1 << " / " << r2 << " = " << divR << endl;

   cout << r1 << " == " << r2 << (r1.eq(r2)?" true":" false") << endl;
   cout << r1 << " < " << r2 << (r1.less(r2) ? " true" : " false") << endl;
   cout << r1 << " != " << r2 << (r1.neq(r2) ? " true" : " false") << endl;
   return 0;
}

//Output

//If you need any help regarding this solution...... please leave a comment...... thanks


Related Solutions

Create a C# Application. Create a class object called “Employee” which includes the following private variables:...
Create a C# Application. Create a class object called “Employee” which includes the following private variables: firstN lastN idNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods: constructor properties CalcPay(): Calculate the regular pay and overtime pay. Create...
# List the two private member variables (including name and functionality) in the node class. #Write...
# List the two private member variables (including name and functionality) in the node class. #Write a general pattern for a loop statement that traverses all the nodes of a linked list
C++ Define a base class called Person. The class should have two data members to hold...
C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and !=...
In C++ please Your class could have the following member functions and member variables. However, it's...
In C++ please Your class could have the following member functions and member variables. However, it's up to you to design the class however you like. The following is a suggestion, you can add more member variables and functions or remove any as you like, except shuffle() and printDeck(): (Note: operators must be implemented) bool empty(); //returns true if deck has no cards int cardIndex; //marks the index of the next card in the deck Card deck[52];// this is your...
white a class having two private variables and one member function which will return the area...
white a class having two private variables and one member function which will return the area of rectangle
myLinkedList.java>>>>>>>> public class MyLinkedList implements MiniList<Integer>{ /* Private member variables that you need to declare: **...
myLinkedList.java>>>>>>>> public class MyLinkedList implements MiniList<Integer>{ /* Private member variables that you need to declare: ** The head pointer ** The tail pointer */ public class Node { // declare member variables (data and next) // finish these constructors public Node(int data, Node next) {} public Node(int data) {} // HINT: use this() with next = null } // Initialize the linked list (set head and tail pointers) public MyLinkedList() {} @Override public boolean add(Integer item) { return false; }...
PLEASE WRITE THIS IN C++ Write a ComplexNumber class that has two member variables realNum and...
PLEASE WRITE THIS IN C++ Write a ComplexNumber class that has two member variables realNum and complexNum of type double. Your class shall have following member functions Complex(); Complex(double, double); Complex add(Complex num1, Complex num2); Complex subtract(Complex num1, Complex num2); string printComplexNumber(); Write a driver program to test your code. Please make sure your code is separated into Complex.h Containing definition of the class Complex.cpp Containing the implementation ComplexTestProgram.cpp Containing main program Use the following directive in class definition to...
C++ Create a class for working with fractions. Only 2 private data members are needed: the...
C++ Create a class for working with fractions. Only 2 private data members are needed: the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3/7 will have the two private data member values of 3 and 7. The following methods should be in your class: a. A default constructor that should use default arguments in case no initializers are included in the main. The fraction needs to be stored in reduced...
Write a C++ programs to: 1. Create a class called Student with four (4) private member...
Write a C++ programs to: 1. Create a class called Student with four (4) private member variables, name (string), quiz, midterm, final (all double type); 2. Include all necessary member functions for this class (at least 1 constructor, 1 get function, and 1 grading function – see #6); 3. Declare three (3) objects of Student type (individual or array); 4. Read from the keyboard three (3) sets of values of name, quiz, midterm, final and assign them to each object;...
Write a rational number class in C++. Recall a rational number is a rational number, composed...
Write a rational number class in C++. Recall a rational number is a rational number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator. A principle of abstract data type construction is that constructors must be present to create objects with any legal values. You should provide the following two constructors: one constructor to make...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT