0% found this document useful (0 votes)
21 views4 pages

Operator Overloading Using Rational Numbers

This document defines a Rational class that represents fractions and overloads various arithmetic and comparison operators to allow rational numbers to be used similarly to built-in numeric types. It includes member functions to perform addition, subtraction, multiplication, division and increment/decrement operations on Rational objects. It also defines extraction and insertion operators for input/output and comparison functions to check if one Rational is greater than or less than another.

Uploaded by

fayazfayi59
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

Operator Overloading Using Rational Numbers

This document defines a Rational class that represents fractions and overloads various arithmetic and comparison operators to allow rational numbers to be used similarly to built-in numeric types. It includes member functions to perform addition, subtraction, multiplication, division and increment/decrement operations on Rational objects. It also defines extraction and insertion operators for input/output and comparison functions to check if one Rational is greater than or less than another.

Uploaded by

fayazfayi59
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<iostream>

using namespace std;


class Rational{
int neum ;
int denum;
public :
void read();
void print();
Rational operator +(Rational);
Rational operator -(Rational);
Rational operator *(Rational);
Rational operator /(Rational);
friend istream& operator >>(istream&,Rational&);
friend ostream& operator <<(ostream&,Rational&);
Rational operator ++();
Rational operator ++(int);
Rational operator --();
Rational operator --(int);
Rational operator +(int);
Rational operator -(int);
Rational operator *(int);
Rational operator /(int);
// Rational operator ++();
// Rational operator --();
int isgreater(Rational);
int islesser(Rational);
};

Rational Rational::operator --(){


neum=neum-denum;
return *this;
}
Rational Rational::operator --(int){

Rational t=*this;

neum=neum-denum;
return t;
}
Rational Rational::operator ++(){
neum=neum+denum;
return *this;

}
Rational Rational::operator ++(int){
Rational t=*this;
neum=neum+denum;
return t;
}

ostream& operator <<(ostream &out,Rational &r){


out<<r.neum<<"/"<<r.denum;
}
istream& operator >>(istream &in,Rational &r){
cout<<"enter neum and denum\n";
in>>r.neum;
in>>r.denum;
}
int Rational::islesser(Rational r){
float f1,f2;
f1=neum/denum;
f2=r.neum/r.denum;
if(f1<f2)
return 0;
else
return 1;
}
int Rational::isgreater(Rational r){
float f1,f2;
f1=neum /denum;
f2=r.neum /r.denum;
if(f1>f2)
return 0;
else
return 1;
}
/*
void Rational::read(){
cout<<"enter neum \n";
cin>>neum;
cout<<"enter denum\n";
cin>>denum;
}
void Rational::print(){
cout<<neum<<"/"<<denum<<"\n";
}*/

Rational Rational::operator +(Rational r){


Rational t;

t.neum=neum*r.denum + denum*r.neum;
t.denum=denum*r.denum;
return t;
}
Rational Rational::operator -(Rational r){
Rational t;
t.neum=neum*r.denum - r.neum*denum;
t.denum=denum*r.denum;
return t;
}
Rational Rational::operator *(Rational r){
Rational t;
t.neum=neum*r.neum;
t.denum=denum*r.denum;
return t;
}
Rational Rational::operator /(Rational r){
Rational t;
t.neum =neum*r.denum;
t.denum=denum*r.neum;
return t;
}

Rational Rational::operator +(int i){


Rational t;
t.neum=neum+i*denum;
t.denum=denum;
return t;
}
Rational Rational::operator -(int i){
Rational t;
t.neum=neum-i*denum;
t.denum=denum;
return t;
}

Rational Rational::operator *(int i){


Rational t;
t.neum=neum*i;
t.denum=denum;
return t;
}
Rational Rational::operator /(int i){
Rational t;
t.neum=neum*i;
t.denum=denum;
return t;
}/*
Rational Rational::operator ++(int k){
Rational t;
t.neum=neum+denum;
t.denum=denum;
return t;
}
Rational Rational::operator --(int k){
Rational t;
t.neum=neum-denum;
t.denum=denum;
return t;
}*/
int main(){
Rational r,r1,r2,r3,r4;
// r1.read();
// cout<<"2nd rational number\n";
// r2.read();
cout<<"enter 1st rational num\n";
cin>>r1;
cout<<"enter 2nd rational num\n";
cin>>r2;
cout<<"output:\n";
cout<<r1<<"\n"<<r2<<"\n";

// cout<<"1.rational operations\n"<<"2.overloading\n"<<"increment and


decrement\n"<<"greater or lesse\n";
// do{
// cout<<"enter your option\n";
// cin>>ch;
// switch(ch){*/
// case 1 :
r=r1+r2;
cout<<"addition of 2 rationals:";
cout<<r<<"\n";
r=r1-r2;
cout<<"sub of 2 rationals:";
cout<<r<<"\n";
r=r1*r2;
cout<<"mul of 2 rationals:";
cout<<r<<"\n";
r=r1/r2;
cout<<"division of 2 rationals:";
cout<<r<<"\n";
// break;
// case 2 :
int ele;
cout<<"enter a num \n";
cin>>ele;
r4=r1+ele;
cout<<"additon of ele and rational:";
cout<<r4<<"\n";
r4=r1-ele;
cout<<"sub of ele and rational:";
cout<<r4<<"\n";
r4=r1*ele;
cout<<"mul of ele and rational:";
cout<<r4<<"\n";
r4=r1/ele;
cout<<"div of ele and rational:";
cout<<r4<<"\n";
// break;
// case 3 :

r4=r1++;
cout<<"pre increment of r4:";
cout<<r4<<"\n";
cout<<"pre increment of r1:";
cout<<r1<<"\n";
r4=r1--;
cout<<"post decrement of r4:";
cout<<r4<<"\n";
cout<<"post decrement of r1:";
cout<<r1<<"\n";
// break;
// case 4 :

if(r1.isgreater(r2)==0)
cout<<"r1 is greater \n";
else
cout<<"r2 is greater\n";
if(r1.islesser(r2)==0)
cout<<"r1 is lesser\n";
else
cout<<"r2 is lesser\n";
// break;
// default : cout<<"invalid\n";
// }
// }while(ch<=4);*/
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy