Question
Need help with overloading functions. Please write the answer in understandbale simple C++ language. Thank you! This assignment is a continuation from our last week's
Need help with overloading functions. Please write the answer in understandbale simple C++ language. Thank you!
This assignment is a continuation from our last week's assignment, I will include last week's assignment below. (I have already finished it, this is not the question.)
"Create a class called Fractionfor performing arithmetic with fractions. Use integer variables to represent the privatedata of the classthe numerator and the denominator. Provide a constructor function that enables an object of this class to be initialized when it is declared. The constructor should containdefault values in case no initializers are provided. Provide public member functions for each of the following:
a) Addition of two Fraction numbers. See the solution as an example in Frac.cpp.
b) Subtraction of twoFraction numbers.
c) Multiplication of twoFraction numbers.
d) Division of two Fraction numbers.
e) PrintingFractionnumbers in the form a/b where a is the numerator and b is the denominator.
f) Printing Fraction numbers in double floating-point format."
And our this week's assignment is based on the assignment from last week. I shall include the requirments below: (This is the one I have problems with and wish for the answer to)
To continue Project 4, you are asked to do the following: In Frac2.h, replace the methods addition( const Fraction & ),subtraction(const Fraction&), multiply(const Fraction &); divide(const Fraction &); void printFraction(); by overloading operators +, -, *, /, << respectively.
Add overloading operators > and < so that they can compare two Fractions.
Create your own implementation file which is named Frac2.cpp to replace Frac.cpp (from lastproject) and implement the Fraction class and the necessary overloading operators.
Download the driver lab5driver.cpp to test your codes. If your codes in files Frac2.h and Frac2.cpp are correct, the standard output after the execution of your program should be like the following:
7/3 + 1/3 = 8/3
7/3 - 1/3 = 2
7/3 * 1/3 = 7/9
7/3 / 1/3 = 7
7/3 is:
> 1/3 according to the overloaded > operator
>= 1/3 according to the overloaded < operator
Press any key to continue
Frac2.h Outline:
// a Fraction object holds one Fraction number, one fraction
#ifndef FRAC_H
#define FRAC_H
#include
using namespace std;
class Fraction { // not fully commented
Public:
Fraction(int = 0, int = 1); // default constructor
Fraction add(const Fraction &);
/* add the prototypes of required methods here
....
*/
void printFractionAsFloat();
Private:
int numerator;
int denominator;
void reduce(); // utility function, reduce to lowest terms};
#endif
Test_Driver.cpp (This is used to test if the code runs correctly)
// driver for project 5 #include "frac2.h"
int main() { Fraction c( 7, 3 ), d( 3, 9 ), x;
// c.printFraction(); cout << c; cout << " + " ; // d.printFraction(); cout << d; cout << " = "; x = c + d; // x.printFraction(); cout << x;
cout << ' '; // c.printFraction(); cout <
cout << " - " ; // d.printFraction(); cout << d; cout << " = "; x = c - d; // x.printFraction(); cout <
cout << ' '; // c.printFraction(); cout << c; cout << " * " ; // d.printFraction(); cout << d; cout << " = "; x = c * d; // x.printFraction(); cout <
cout << ' '; // c.printFraction(); cout < cout << " / " ; // d.printFraction(); cout << d; cout << " = "; x = c / d; // x.printFraction(); cout << x;
cout << ' '; // c.printFraction(); cout << c; cout << " is: ";
cout << ( ( c > d ) ? " > " : " <= " ); // d.printFraction(); cout << d; cout << " according to the overloaded > operator ";
cout << ( ( c < d ) ? " < " : " >= " ); // d.printFraction(); cout << d; cout << " according to the overloaded < operator ";
return 0; }
I need help with the header file "frac2.h" and the implementation file "frac2.cpp", please comment if you need anymore information. Thank you for taking your time to help me!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started