Answer to Question #213029 in C++ for Hemambar

Question #213029

 Create a class Rational whose object can store a rational number. (For ex: ¾). Include the necessary constructors to initialize the objects and function to display the rational number in x/y format. Define the following non-member functions. 

addRational(Rational r1, Rational r2) : which should return a summation of the two input rational numbers r1 and r2. 

mulRational(Rational r1, Rational r2) : which should return the product of the r1 and r2. Set these two functions as friend to the Rational class 

Main function to read two rational numbers and display the sum and product of the input by calling the proper functions




1
Expert's answer
2021-07-13T04:30:44-0400
[13:38, 13/07/2021] Divya Airtel: #include<iostream> 
using namespace std;


class Rational 
{ 
    private: 
    int n,d; 
    public: 
            
		void input() 
        { 
        	cout<<"Enter value of numerator : ";
        	int a;
        	cin>>a;
        	cout<<"Enter value of denominator : ";
        	int b;
        	cin>>b;
            n=a; 
            d=b; 
        } 
            
		void addRational(Rational r1, Rational r2) 
        { 
            n=r1.n*r2.d+r2.n*r1.d; 
            d=r1.d*r2.d; 
        } 
        
        void mulRational(Rational r1, Rational r2)
        {
        	n = r1.n * r2.n;
        	d = r1.d * r2.d;
		}
		
		void display()
		{
			cout<<n<<"/"<<d<<endl;
		}
 }; 
 
 
 int main()
 {
 	
 	Rational r1, r2, r3, r4;
 	r1.input();
 	cout<<endl<<endl;
 	r2.input();
 	
 	cout<<endl<<endl;
 	r4.addRational(r1, r2);
 	cout<<"Addition of entered rational numbers is : ";
 	r4.display();
 	cout<<endl;
 	cout<<"Multiplication of entered rational numbers is : ";
 	r3.mulRational(r1, r2);
 	r3.display();
 	
 	
 	
 }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS