In: Computer Science
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.
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.