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...
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++ 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.
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?...
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?
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
This chapter uses the class rectangleType to illustrate how to overload the operators +, *, ==,...
This chapter uses the class rectangleType to illustrate how to overload the operators +, *, ==, !=, >>, and <<. In this exercise, first redefine the class rectangleType by declaring the instance variables as protected and then overload additional operators as defined in parts a to c. 1.Overload the pre- and post-increment and decrement operators to increment and decrement, respectively, the length and width of a rectangle by one unit. (Note that after decrementing the length and width, they must...
Lab Assignment Objectives 'Be able to overload combined binary operators as member operator functions. Show how...
Lab Assignment Objectives 'Be able to overload combined binary operators as member operator functions. Show how to overload binary operators as friend functions. Show how to convert from a fundamental type to a user-defined type using a constructor. Understand exception handling mechanisms using try-catch block statements. Understand the Application Complex Numbers A complex number, c, is an ordered pair of real numbers (doubles). For example, for any two real numbers, s and t, we can form the complex number: This...
Lab Assignment Objectives 'Be able to overload combined binary operators as member operator functions. Show how...
Lab Assignment Objectives 'Be able to overload combined binary operators as member operator functions. Show how to overload binary operators as friend functions. Show how to convert from a fundamental type to a user-defined type using a constructor. Understand exception handling mechanisms using try-catch block statements. Understand the Application Complex Numbers A complex number, c, is an ordered pair of real numbers (doubles). For example, for any two real numbers, s and t, we can form the complex number: This...
In class, we discussed the security market line (SML). Explain this concept to your friend using...
In class, we discussed the security market line (SML). Explain this concept to your friend using an appropriate diagram? If a security's expected return versus risk is plotted above or below the SML what does this imply?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT