Question
Use a template so the class works with any kind of number (i.e. int, float, double) fractionType num1(5, 6); fractionType num1(5.1, 6.2); Tip: get your
Use a template so the class works with any kind of number (i.e. int, float, double)
fractionType
fractionType
Tip: get your class working with integer values first for numerator and denominator and convert to a template after it is working with ints. When converting to a template you may need to move all of your implementations into the .h header file. Instead of creating function definitions in the header and putting the implementations in the .cpp file, you wont even have a .cpp. File. Just put the function implementation inside the header file - no need for a function definition if you do this.
i.e. Typical definition in the .h file
bool operator
resulting implementation in the .cpp file
bool fractionType::operator
{
return(numerator / denominator
}
Simply eliminate the definition in the .h and replace it with the function itself.
i.e. class definition
template class T>
class fractionType
your private class variables......
private:
T numerator; //variable to store the numerator
T denominator; //variable to store the denominator
and an example ...
bool operator rightFr) const {
return (numerator * rightFr.denominator
denominator * rightFr.numerator);
}
IMPORTANT: The above are just samples of a portion of the fraction class - you are required to add templating to the entire fraction class.
Below is a template of the class given to you with all of the functions empty - you need to make sure your fraction class implements all of the folloeing:
#ifndef H_fraction
#define H_fraction
#include
using namespace std;
template class T>
class fractionType
{
// overload stream insertion and extraction operators
friend ostream& operatorconst fractionType
}
friend istream& operator>> (istream& is, fractionType
}
public:
//Constructor
fractionType(T num = 0, T den = 1) {
}
//overload +
fractionType
}
//overload *
fractionType
}
//overload -
fractionType
}
//overload /
fractionType
}
//overload relational operators
bool operator==(fractionType
}
bool operator!=(fractionType
}
bool operator rightFr) const {
}
bool operator rightFr) const {
}
bool operator>=(fractionType
}
bool operator>(fractionType
}
private:
T numerator; //variable to store the numerator
T denominator; //variable to store the denominator
};
#endif
10. Rational fractions are of the form a/b, in which a and b are integers and b=0. In this exercise, by "fractions" we mean rational fractions. Suppose a/b and c/d are fractions. Arithmetic operations on fractions are defined by the following rules: a/b+c/d=(ad+bc)/bda/bc/d=(adbc)/bda/bc/d=ac/bd(a/b)/(c/d)=ad/bc;inwhichc/d=0. Fractions are compared as follows: a/bopc/d if adopbc, in which op is any of the relational operations. For example, a/b
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