Aim : Create a class rational which represents a numerical value by two double values
NUMERATOR and DENOMINATOR. Include the following public member functions:
constructor with no arguments (default), constructor with two arguments, void reduce ()
that reduces the rational number by eliminating the highest common factor between the
numerator and denominator.
Overload + operator to add two rational numbers
Overload - operator to enable input through cin
Overload * operator to enable output through cout
Write a main ( ) to test all the functions in the class.
Theory Operator Overloading:
Theory: C++ Operator Overloading
C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. Operator overloading is a compile-time polymorphism. For example, we can overload an operator ‘+’ in a class like String so that we can concatenate two strings by just using +. Other example classes where arithmetic operators may be overloaded are Complex Numbers, Fractional Numbers, Big integers, etc.
Syntax:
class A {
statements;
};
int main()
{
A a1, a2, a3;
a3 = a1 + a2;
return 0;
}
Program :
Conclusion : Hence we have performed our
practical successfully