Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(write in C++ language) A rational number is of the form a/b where a and b are i

ID: 3890071 • Letter: #

Question

(write in C++ language)

A rational number is of the form a/b where a and b are integers with b not equal to zero. Develop a class for processing rational numbers. The class should have a numerator data member and a denominator data member. It should read and display all rational numbers in the format a/b; for output, just display a if the denominator is 1. The following examples illustrate the operations that should be provided:

A rational number x can be entered in the form

a or a/b. (e.g. 4, 3/4, 5/2)

x + y addition

x - y subtraction

x * y multiplication

x / y division

Explanation / Answer

class rationalnum {
//Access - Specifier
public:
//Variable Declaration
int numerator;
int denominator;
};

//Main Function

int main() {
// Object Creation For Class
rationalnum obj;

//Get Input Values For data members
cout << "Enter the numerator :";
cin >> obj.numerator;

cout << "Enter the denominator";
cin >> obj.denominator;

//Show the Output
if(obj.denominator!=0 && obj.numerator.denominator==1)
cout << obj.numerator << "/" << obj.denominator << endl;
else if(obj.denominator==1)cout <<obj.numerator<<endl;
getch();
return 0;
}

The class structure is very well explained through comments, Declare an object which is a variable of type classname . Here object is obj . All the data members of the class are accessed by object name.