I need help writing this copy constructor A constructor that takes a numerator a
ID: 3862933 • Letter: I
Question
I need help writing this copy constructor
A constructor that takes a numerator and denominator and guarantees that the resulting object holds a valid rational number (i.e., the denominator cannot be 0). Note that these numerator and denominator may need to be adjusted during initialization in order to normalize the representation to support other operations (such as 2/4 -> 1/2; 1/ -2 -> -1/2;6/3-> 2/1; 8/6-> 4/3;etc.)
I have a class Rational made. I know its going to start with
Rational(int num, int denom){
if(denomr == 0)
std::cout<< "The number is invalid";
else
int rat = lcm(num, denom);
}
Im given a function that finds the gcd(greatest common denominator) and lcm(least common mulitple) but im not sure how use them. The lcm does use absolute value so i know thats needed to work with the negatives and gcd is used in reducing. im also writing in c++
Explanation / Answer
Rational(int num, int denom){
if(denomr == 0)
std::cout<< "The number is invalid";
else
int rat = gcd(num, denom);
if(denom < 0)
{
denom *= -1; num *= -1;
}
this->num = num/rat;
this->denom = denom/rat;
}