Question

In: Computer Science

In class we discussed how to overload the + operator to enable objects of type Fraction to be added together using the + operator.

2.(a) Fraction Operators Objective:

In class we discussed how to overload the + operator to enable objects of type Fraction to be added together using the + operator. Extend the Fraction class definition so that the -, * and / operators are supported. Write a main function that demonstrates usage of all of these operators.

(b)More Custom Types Objective:

Define a set of classes that represent a Schedule and Course in the context of a university student who has a schedule with a set of courses that they are enrolled in. You may define whichever properties you think are best for such a data type. Demonstrate usage of these data types in a main function.


Solutions

Expert Solution

Code

Fraction.h

#ifndef FRACTION_H
#define FRACTION_H
#include
using namespace std;
class Fraction
{
   friend ostream &operator<<(ostream&, Fraction&);
public:
   Fraction(int, int);
   Fraction();
   //Fraction &operator=(Fraction&);
   Fraction operator+(const Fraction&);
   Fraction operator-(const Fraction&);
   Fraction operator *(const Fraction&);
   Fraction operator /(const Fraction&);
   void reduction();
private:
   int numerator;
   int denominator;
};
#endif // ! FRACTION_H

Fraction.cpp implementaion file

#include"Fraction.h";
Fraction::Fraction(int numerator, int denominator)
{
   this->numerator = numerator;
  
   if (denominator == 0)
   {
       this->denominator = 1;
   }// end if
   else
       this->denominator = denominator;
   reduction();
}
Fraction::Fraction()
{
   this->numerator = 0;
   this->denominator = 1;
}

ostream &operator<<(ostream &out, Fraction &number)
{
   out << number.numerator << '/' << number.denominator;

   return out;
}
void Fraction::reduction()
{
   int largest;
   largest = numerator > denominator ? numerator : denominator;
   int gcd = 0; // greatest common divisor
   for (int loop = 2; loop <= largest; loop++)
       if (numerator % loop == 0 && denominator % loop == 0)
           gcd = loop;
   if (gcd != 0)
   {
       numerator /= gcd;
       denominator /= gcd;
   } // end if
} // end function
Fraction Fraction::operator+(const Fraction& c1)
{
   Fraction temp;
   temp.numerator = (numerator*c1.denominator) + (c1.numerator*denominator);
   temp.denominator = denominator * c1.denominator;
   temp.reduction();
   return temp;
}
Fraction Fraction::operator-(const Fraction& c1)
{
   Fraction temp;
   temp.numerator = (numerator*c1.denominator) - (c1.numerator*denominator);
   temp.denominator = denominator * c1.denominator;
   temp.reduction();
   return temp;
}
Fraction Fraction::operator*(const Fraction& c1)
{
   Fraction temp;
   temp.numerator = (numerator*c1.numerator);
   temp.denominator = denominator * c1.denominator;
   temp.reduction();
   return temp;
}
Fraction Fraction::operator/(const Fraction& c1)
{
   Fraction temp;
   temp.numerator = (numerator*c1.denominator);
   temp.denominator = denominator * c1.numerator;
   temp.reduction();
   return temp;
}

main.cpp

#include"Fraction.h"

int main()
{
   Fraction f1(1, 2);
   Fraction f2(1, 3);
   Fraction f3;
  
   cout << "Ther two fraction enterd are " << endl;
   cout << "f1 = " << f1 << endl;
   cout << "f2 = " << f2 << endl;

   cout << "The arithmetic operations on these two fractions:" << endl;
   f3 = f1 + f2;
   cout << "f1 + f2 = " << f3 << endl;
   f3 = f1 - f2;
   cout << "f1 - f2 = " << f3 << endl;
   f3 = f1 * f2;
   cout << "f1 * f2 = " << f3 << endl;
   f3 = f1 / f2;
   cout << "f1 / f2 = " << f3 << endl;

   system("pause");
   return 1;
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Using the program below, overload the Multiply operator to the Rectangle class… this will multiple the...
Using the program below, overload the Multiply operator to the Rectangle class… this will multiple the widths for the 2 Rectangles and multiple the lengths of the two rectangles (similar to how the “+” operator added the widths and lengths of the 2 rectangles). Overload this operator using the non-member method. Please copy-paste code into MS Word document and then show some screenshots of you running and testing this method using your IDE. Using the program below, overload the Divide...
c++ using class... define operator overloading and give simple example how we can use operator overloading...
c++ using class... define operator overloading and give simple example how we can use operator overloading by writing simple program in which different operators are used to add, subtract, multiply and division.
Complete the following: Extend the newString class (attached) to include the following: Overload the operator +...
Complete the following: Extend the newString class (attached) to include the following: Overload the operator + to perform string concatenation. Overload the operator += to work as shown here: s1 = "Hello " s2 = "there" s1 += s2 // Should assign "Hello there" to s1 Add a function length to return the length of the string. Write a test program. //myString.h (header file) //Header file myString.h    #ifndef H_myString #define H_myString #include <iostream> using namespace std; class newString {...
C++ Class involving Set intersection The goal is to overload the function: void Bag::operator/=(const Bag& a_bag)...
C++ Class involving Set intersection The goal is to overload the function: void Bag::operator/=(const Bag& a_bag) // Bag<int> bag1 = 1,2,3,4 //Bag<int> bag2 = 2,5,6,7 bag1+=bag2; bag1.display() should return 2 //Since type is void, you should not be returning any array but change the bag itself. #include <iostream> #include <string> #include <vector> using namespace std; template<class T> class Bag { public: Bag(); int getCurrentSize() const; bool isEmpty() const; bool add(const T& new_entry); bool remove(const T& an_entry); /** @post item_count_ ==...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not reduce fractions, do not use "const," do not provide any constructors, do not use three separate files. You will not receive credit for the assignment if you do any of these things. In your single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. It is required that you provide these member...
Biochemistry: Enzyme Units Can relative activity be added together? For example, if fraction A has 0.2...
Biochemistry: Enzyme Units Can relative activity be added together? For example, if fraction A has 0.2 U/mL and fraction B has 0.4 U/mL, will combining the two fractions produce a solution with a relative activity of 0.6 U/mL?
In class, we discussed problems associated with the supply of physicians in Alabama, and how these...
In class, we discussed problems associated with the supply of physicians in Alabama, and how these problems affect the health care market in Alabama. Your textbook also reviews the physician supply in the USA. The demand for physicians has been much greater than the supply, especially primary care physicians in rural areas of Alabama. What are three of the problems we discussed with the supply of physicians and what are three solutions that would help to overcome the supply problem?...
C++ Class involving union. The goal is to overload the function: void Bag<T>::operator+=(const Bag<T>& a_bag) //...
C++ Class involving union. The goal is to overload the function: void Bag<T>::operator+=(const Bag<T>& a_bag) // The union of two sets A and B is the set of elements which are in A, in B, or in both A and B. For instance, Bag<int> bag1 = 1,2,3 and Bag<int> bag2 = 3,4,5 then bag1+=bag2 should return 1,2,3,4,5. //Since type is void, it should not return an array. #include <iostream> #include <string> #include <vector> using namespace std; template<class T> class Bag...
As we discussed in class, the US Treasury bond rate is determined using an auction process....
As we discussed in class, the US Treasury bond rate is determined using an auction process. Thoroughly discuss how the bond auction process operates
Hello, I need this is C++. Thank you! (1B) Write a Fraction class whose objects will...
Hello, I need this is C++. Thank you! (1B) Write a Fraction class whose objects will represent Fractions. Note: this is the first part of a multi-part assignment. For this week you should not simplify (reduce) fractions, you should not use "const," and all of your code should be in a single file. In this single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. You must provide...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT