C++ class program to perform rational number arithmetic

Write a program to perform rational number arithmetic.

 #include<stdio.h> 	
 #include<iostream.h>
 #include<conio.h>
 class rational 
 {
	 int numer;
	 int denom;
	 public:
	 void getdata()
	 {
		 cout<<"\n enter the numerator part of the rational no.";
		 cin>>numer;
		 cout<<"\n enter the denominator part of the rational no.";
		 cin>>denom;
	 }
	 void operator+(rational);
	 void operator-(rational);
	 void operator *(rational);
	 void operator /(rational);
 };
 void rational ::operator+(rational c1)
 {
	 rational temp;
	 temp.numer=(numer*c1.denom)+(c1.numer*denom);
	 temp.denom=denom*c1.denom;
	 cout<<"\nrational no. after addition";
	 cout<<"\n numerator="<<temp.numer<<"\n denominator ="<<temp.denom;
 }
 void raional ::operator -(rational c1)
 {
	 rational temp;
	 temp.numer=(numer*c1.denom)-(c1.numer*denom);
	 temp.denom=denom*c1.denom;
	 cout<<"\n rational no. after subtraction";
	 cout<<"\n numerator="<<temp.numer<,"\n denominator ="<<temp.denom;
 }
 void rational ::operator (rational c1)
 {
	 rational temp;
	 temp.numer=numer*c1.numer;
	 temp.denom=denom*c1.denom;
	 cout<<"\n rational no. after multiplication";
	 cout <<"\n numerator="<temp.numer<<"\n denominator ="<< temp.denom;
 }
 void rational :: operator /(rational c1)
 {
	 rational temp;
	 temp.numer= numer*c1.denom;
	 temp.denom=c1.numer*denom;
	 cout<<"\n rational no. after dividation";
	 cout <<"\n numerator="<<temp.numer<<"\n denominator ="<<temp.denom;
 }
 void main()
 { 
	 clrscr();
	 rational c1, c2;
	 int n;
	 do 
	 {
		 cout<<"\n 1.Input data for rational no. ";
		 cout<<"\n 2. Addition of rational no. "; 
		 cout<<"\n 3. Subtraction of rational no. ";
		 cout<<"\n 4. Multiplication of rational no.";
		 cout<<\n  5. Division of rational no. ";
		 cout<<"\n 6. Quit";
		 cout<<"\n Enter your choice";
		 cin>>n;
		 switch(n)
		 {
			 case 1:
			 cout<<endl<<"\n enter the data for first rational no.";
			 c1.getdata();
			 cout<<endl<<"\n enter the data for second rational no. ";
			 c2.getdata ();
			 clrscr();
			 break;
			 case 2;
			 c1+c2;
			 getch();
			 clrscr();
			 break;
			 case 3;
			 c1-c2;
			 getch();
			 clrscr();
			 case 4: 
			 c1*c2;
			 getch();
			 clrscr();
			 break;
			 case 5:
			 c1/c2;
			 getch();
			 clrscr();
			 break;
			 case 6:
			 exit(1);
			 break;
		 }
	 } while (n!=6);
	 getch();
 }

3 Responses to “C++ class program to perform rational number arithmetic”

  1. mahdi

    Hello, Good Friend
    The error codes in this section are:

    void rational :: operator (rational c1)

    MY C++: turbo c++ – version 3 – compiler:borland

    THANK YOU

    VERY VERY GOOD

    Reply

Leave a Reply