#include<iostream>
using namespace std;
class RationalNumber //here i make a class name RationalNumber
{
private:
int P,Q;
public:
RationalNumber () //here i define the default constructor
{
P=0;
Q=0;
}
RationalNumber (int a,int b) //here i define a parameterized constructor
{
P=a;
Q=b;
}
void display() //here i define a function that display the values
{
cout<<P<<"/"<<Q<<endl;
}
RationalNumber operator +(RationalNumber o1); //here i overload the + operator
RationalNumber operator -(RationalNumber o2); //here i overload the - operator
RationalNumber operator *(RationalNumber o3); //here i overload the * operator
RationalNumber operator /(RationalNumber o4); //here i overload the / operator
friend bool operator==(RationalNumber &p1, RationalNumber &p2); //here i overload the == operator
friend bool operator!=(RationalNumber &p1, RationalNumber &p2);//here i overload the != operator
friend bool operator>=(RationalNumber &p1, RationalNumber &p2); //here i overload the >= operator
friend bool operator<=(RationalNumber &p1, RationalNumber &p2);//here i overload the <= operator
};
RationalNumber RationalNumber ::operator +(RationalNumber o1) //here i overload the + operator
{
RationalNumber result;
result.P=P*o1.Q+o1.P*Q;
result.Q=Q*o1.Q;
return result;
}
RationalNumber RationalNumber ::operator -(RationalNumber o2) //here i overload the - operator
{
RationalNumber result;
result.P=P*o2.Q-o2.P*Q;
result.Q=Q*o2.Q;
return result;
}
RationalNumber RationalNumber::operator *( RationalNumber o3) //here i overload the * operator
{
RationalNumber result;
result.P=P*o3.Q;
result.P=P*o3.Q;
return result;
}
RationalNumber RationalNumber::operator /(RationalNumber o4) //here i overload the / operator
{
RationalNumber result;
result.P=P/o4.P;
result.Q=Q/o4.Q;
return result;
}
bool operator==(RationalNumber&p1, RationalNumber &p2)//here i overload the == operator
{
return (p1.P==p2.P||p1.Q==p2.Q);
}
bool operator!=(RationalNumber&p1, RationalNumber &p2)//here i overload the != operator
{
return (p1.P!=p2.P||p1.Q!=p2.Q);
}
bool operator>=(RationalNumber&p1, RationalNumber &p2)//here i overload the >= operator
{
return (p1.P>=p2.P||p1.Q>=p2.Q);
}
bool operator<=(RationalNumber&p1, RationalNumber &p2)//here i overload the <= operator
{
return (p1.P<=p2.P||p1.Q<=p2.Q);
}
int main()
{
RationalNumber p1,p2,p3;
cout<<"\t\twhen calling first object"<<endl;
p1=RationalNumber (6,7);
p1.display();
cout<<endl;
cout<<"\t\twhen calling second object"<<endl;
p2=RationalNumber (3,4);
p2.display();
cout<<endl;
p3=p1+p2;
cout<<"\t\tafter add the two RationalNumber object:"<<endl;
p3.display();
cout<<endl;
p3=p1-p2;
cout<<"\t\tafter subtract the two RationalNumber object:"<<endl;
p3.display();
cout<<endl;
p3=p1*p2;
cout<<"\t\tafter multiply the two RationalNumber object:"<<endl;
p3.display();
cout<<endl;
p3=p1/p2;
cout<<"\t\tafter divide the two RationalNumber object:"<<endl;
p3.display();
cout<<endl;
cout<<"\tafter checking the equality between two rational numbers:"<<endl;
if(p1 == p2)
{
cout << "Both the are equal";
}
cout<<endl;
cout<<"\tafter checking the unequality between two rational numbers:"<<endl;
if(p1 != p2)
{
cout << "Both the are not equal";
}
cout<<endl;
cout<<"\tafter checking this >=:"<<endl;
if(p1 >= p2)
{
cout<<"p1 is greater then and equal to p2"<<endl;
}
cout<<endl;
cout<<"\tafter checking this <=:"<<endl;
if(p1 <= p2)
{
cout <<"p2 less then and equal to p1";
}
}
No comments:
Post a Comment