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...
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 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...
C++ PersonData and CustomerData classes Design a class named PersonData with the following member variables: lastName...
C++ PersonData and CustomerData classes Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData, which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool. It will...
C++ please Create a Stats class whose member data includes an array capable of storing 30...
C++ please Create a Stats class whose member data includes an array capable of storing 30 double data values, and whose member functions include total, average, lowest, and highest functions for returning information about the data to the client program. These are general versions of the same functions you created for Programming Challenge 7, but now they belong to the Stats class, not the application program. In addition to these functions, the Stats class should have a Boolean storeValue function...
directions: use c++ Create a  ContactInfo class that contains the following member variables: name age phoneNumber The...
directions: use c++ Create a  ContactInfo class that contains the following member variables: name age phoneNumber The ContactInfo class should have a default constructor that sets name = "", phoneNumber = "", and age = 0. The ContactInfo class should have a constructor that accepts the name and phone number as parameters and sets name = <value of parameter name>, aAge = 0, and phoneNumber = <value of parameter phone number>. The ContactInfo class should have accessor and mutator functions for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT