In: Computer Science
C++
Question1. Create a class called Rantional for performing arithmatic with fractions. Then write a program to test your class. Use integer variables to represent the private data of the class, meaning the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in
case no initializers are provided and should store the fraction
in reduced form. For example fraction 2/4 would be stored in the
object as 1 for the numerator and 2 for the denominator.
Note: Remember that denominator cannot be 0.
Provide public member functions that perform each of the following
tasks:
a) add -- Adds 2 rational numbers. Result should be stored in
reduced form.
b) subtract -- Subtracts 2 rational numbers. Store result in
reduced form.
c) multiply -- Multiplies 2 rational numbers. Store result in
reduced form.
Write a main() function to test above functionalites.
Question1 : Create a class called Rantional for performing arithmatic with fractions.
Provide public member functions that perform each of the
following tasks:
a) add -- Adds 2 rational numbers. Result should be stored in
reduced form.
b) subtract -- Subtracts 2 rational numbers. Store result in
reduced form.
c) multiply -- Multiplies 2 rational numbers. Store result in
reduced form.
Write a main() function to test above functionalites.
PROGRAM:
#include "Rational.h"
#include <iostream>
int main(){
Rational r1(3,4);
r1.print();
r1.printFloat();
std::cout << "============================="
<<std::endl;
Rational r2(5,6);
r2.print();
r2.printFloat();
std::cout << "============================="
<<std::endl;
std::cout <<"The addition is: " <<
std::endl;
Rational ans = r1.add(r2);
ans.print();
ans.printFloat();
std::cout << "============================="
<<std::endl;
std::cout <<"The multiplication is: " <<
std::endl;
ans = r1.mult(r2);
ans.print();
ans.printFloat();
std::cout << "============================="
<<std::endl;
return 0;
};