Search This Blog

Wednesday 24 February 2016

Create a Rational class as follows, Class rational{ p ublic: rational ( ) { }; private : int num; //numerator int den; //denominator }; Implement the +, /,*, - operators for the rational class.

Practical 21



#include<iostream>
using namespace std;
class rational
{
    int num,den;
    public:
    rational(int a,int b){num=a;den=b;}
    friend void operator+(rational a,rational b);
    friend void operator-(rational a,rational b);
    friend void operator*(rational a,rational b);
    friend void operator/(rational a,rational b);
};
void operator+(rational a,rational b)
{
    int x,y;
    x=a.num*b.den+a.den*b.num;
    y=a.den*b.den;
    cout<<x<<"/"<<y<<endl;
}
void operator-(rational a,rational b)
{
    int x,y;
    x=a.num*b.den - a.den*b.num;
    y=a.den*b.den;
    cout<<x<<"/"<<y<<endl;
}
void operator*(rational a,rational b)
{
    int x,y;
    x=a.num*b.num;
    y=a.den*b.den;
    cout<<x<<"/"<<y<<endl;
}
void operator/(rational a,rational b)
{
    int x,y;
    x=a.num*b.den;
    y=a.den*b.num;
    cout<<x<<"/"<<y<<endl;
}
int main()
{
    rational  a(6,8),b(8,6);
    a+b;
    a-b;
    a*b;
    a/b;
    return 0;
}


No comments:

Post a Comment