C++: Given the rational class interface below Note that most of the pre/post-con
ID: 3580143 • Letter: C
Question
C++: Given the rational class interface below Note that most of the pre/post-conditions for the functions (friend or member) are intentionally omitted. Whenever applicable, you must place them underneath each function prototype.
class rational {
friend ostream& operator<<(ostream &, const rational &robj);
// Postcondition: robj is displayed as: 1/4 (not 2/8), -2/3 (not 2/-3), 0 (not 0/5), etc.
friend istream& operator>>(istream &, rational &robj);
// displays prompt message "Enter values for numerator and denominator: "
// Postcondition: two values entered are read and stored in num and denom of robj.
public:
rational();
rational(int, int);
rational operator+(const rational &) const;
rational operator-(const rational &) const;
rational operator*(const rational &) const;
rational operator/(const rational &) const;
rational operator+=(const rational &);
bool operator<(const rational &);
// Postcondition: returns true if calling object is smaller; returns false otherwise
private:
int GCD();
// Postcondition: returns the greatest common divisor between num and denom
int num;
int denom;
};
Implement two friend functions.
Implement all eight member functions.
Write a main function to test all member functions.
Your program output should be similar to the following sample display. Notice that r2 is initialized by the second constructor to a value of 1/3 (i.e., 0) and r1 is declared with no parameter thus it is initialized to 0/1 by calling the default constructor. Your program output should be similar to the following sample display:
CHWindows system32 cmd.exe r1 is initialized by default constructor r1 0/1 r2 is initialized by the 2nd constructor r2 E 1/3 Calling overloaded operator to reset r1 Enter values for numerator followed by denominator: 2 5 After reset r1 2/5 Testing overloade arithmetic and insertion operator r1 r2 2/5 1/3 E 11 /15 r1 r2 2/5 1 /3 1 15 2/5 x 1/3 2/15 r1 /r2 6/5 Press any key to continueExplanation / Answer
#include<iostream>
using namespace std;
//Class rational defined
class rational
{
//Overloading output stream <<
friend ostream& operator<<(ostream &ops, const rational &robj)
// Postcondition: robj is displayed as: 1/4 (not 2/8), -2/3 (not 2/-3), 0 (not 0/5), etc.
{
//If denominator is negative
if(robj.denom < 0)
{
ops<<-robj.num<<"/"<<-robj.denom;
}
//If numerator is 0
else if(robj.num == 0)
{
ops<<0;
}
//If it is divisible
else if((robj.denom % robj.num) == 0)
{
ops<<1<<"/"<<(robj.denom / robj.num );
}
//Otherwise
else
ops<<robj.num<<"/"<<robj.denom;
return ops;
}
//Overloading Input stream operator >>
friend istream& operator>>(istream &ins, rational &robj)
// displays prompt message "Enter values for numerator and denominator: "
// Postcondition: two values entered are read and stored in num and denom of robj.
{
cout<<" Enter the values for the numerator followed by denominator: ";
ins>>robj.num>>robj.denom;
return ins;
}
public:
//Default constructor
rational()
{
num = 0;
denom = 1;
}
//Parameterized constructor
rational(int n, int d)
{
num = n;
denom = d;
cout<<" r2 is initialized by the 2nd constructor: r2 = "<<num<<"/"<<denom;
}
//Overloading + operator
rational operator+(const rational &sec) const
{
//Creates a temporary object
rational temp;
//Calculates the denominator
temp.denom = denom * sec.denom;
//Calculates the numerator
temp.num = (num * (temp.denom / denom)) + (sec.num * (temp.denom / sec.denom));
//Returns the result object
return temp;
}
//Overloading - operator
rational operator-(const rational &sec) const
{
//Creates a temporary object
rational temp;
//Calculates the denominator by multiplying both the denominators
temp.denom = denom * sec.denom;
//Calculates the numerator
temp.num = (num * (temp.denom / denom)) - (sec.num * (temp.denom / sec.denom));
//Returns the result object
return temp;
}
//Overloading * operator
rational operator*(const rational &sec) const
{
//Creates a temporary object
rational temp;
//Calculates the denominator by multiplying both the denominators
temp.denom = denom * sec.denom;
//Calculates the numerator by multiplying both the numerators
temp.num = (num * sec.num);
//Returns the result object
return temp;
}
//Overloading / operator
rational operator/(const rational &sec) const
{
//Creates a temporary object
rational temp;
//Swaps the numerator and denominator of the second object
temp.num = sec.denom;
temp.denom = sec.num;
//Calculates the numerator by multiplying both the numerators
temp.num = (num * temp.num);
//Calculates the denominator by multiplying both the denominators
temp.denom = (denom * temp.denom);
//Returns the result object
return temp;
}
//Overloading += short hand operator operator
rational operator+=(const rational &sec)
{
//Creates a temporary object
rational temp;
//Initializes the member to zero
temp.num = 0;
temp.denom = 0;
//Calculates the denominator by multiplying both the denominators
temp.denom += denom * sec.denom;
//Calculates the numerator
temp.num += (num * (temp.denom / denom)) + (sec.num * (temp.denom / sec.denom));
//Returns the result object
return temp;
}
//Overloading < relational operator operator
bool operator<(const rational &sec)
{
float result1, result2;
//Divides the first numerator and denominator and stores the result in result1
result1 = float(num/denom);
//Divides the second numerator and denominator and stores the result in result2
result2 = float(sec.num/sec.denom);
//Compares both the result
if(result1 < result2)
return true;
else
return false;
}
//Displays the GCD of numerator and denominator.
//Though GCD is private member we need a public member to call this
void disp()
{
//Creates temporary object
rational t;
//Accept data for the object
cin>>t;
//Displays the numerator and denominator
cout<<" GCD of "<<t.num<<" and "<<t.denom<<" = ";
//Calls and displays the GCD
cout<<t.GCD();
}
// Postcondition: returns true if calling object is smaller; returns false otherwise
private:
//Calculates the GCD of numerator and denominator.
int GCD()
{
//Loops till numerator is not equal to denominator
while(num != denom)
{
//If numerator is greater than the denominator
if(num > denom)
num -= denom;
//If numerator is not greater than the denominator
else
denom -= num;
}
return num;
}
// Postcondition: returns the greatest common divisor between num and denom
int num;
int denom;
};
int main()
{
//Calls the default constructor
rational r1;
cout<<" r1 is initialized by the default constructor: r1 = 0/1";
//Calls the parameterized constructor
rational r2(1, 3);
cout<<" Calling overloaded operator >> to reset r1: ";
//Calls the overloading operator >>
cin>>r1;
cout<<" After reset, r1 = "<<r1;
cout<<" Testing overload arithmetic and insertion operator <<: ";
//Calls the overloading operator +
cout<<" r1 + r2 = "<<r1<<" + "<<r2<<" = "<<r1+r2;
//Calls the overloading operator -
cout<<" r1 - r2 = "<<r1<<" - "<<r2<<" = "<<r1-r2;
//Calls the overloading operator *
cout<<" r1 * r2 = "<<r1<<" * "<<r2<<" = "<<r1*r2;
//Calls the overloading operator ?
cout<<" r1 / r2 = "<<r1<<" / "<<r2<<" = "<<r1/r2;
//Calls the overloading operator +=
cout<<" r1 += r2 = "<<r1<<" += "<<r2<<" = "<<(r1+=r2);
//Calls the overloading operator <
string res = (r1 < r2)? "True" : "False";
cout<<" r1 > r2 = "<<r1<<" > "<<r2<<" = "<<res;
r2.disp();
}
Output:
r1 is initialized by the default constructor: r1 = 0/1
r2 is initialized by the 2nd constructor: r2 = 1/3
Calling overloaded operator >> to reset r1:
Enter the values for the numerator followed by denominator: 2 5
After reset, r1 = 2/5
Testing overload arithmetic and insertion operator <<:
r1 + r2 = 2/5 + 1/3 = 11/15
r1 - r2 = 2/5 - 1/3 = 1/15
r1 * r2 = 2/5 * 1/3 = 2/15
r1 / r2 = 2/5 / 1/3 = 6/5
r1 += r2 = 2/5 += 1/3 = 11/15
r1 > r2 = 2/5 > 1/3 = False
Enter the values for the numerator followed by denominator: 78
52
GCD of 78 and 52 = 26
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.