hello!!!
I am new to this site.can anyone help me in my program.It is of rational number class in c++ having operator overloading.It is not executing and i am not able to find mistakes.
My teacher told me that all functions in it should be cascadable.
plz plz point out my mistakes.I have to submit it on monday.
#include<iostre am.h>
#include<conio. h>
enum bool { false,true};
class Rational{
int num;//numerator
int denom;//denomenator
int whl;//whole number
public:
Rational();//constructor
Rational& setRational (int a,int b, int c);//setting the rational numbers
void setNumerator(in t);
void setDenominator( int);
void setwholenumber( int);
int getNumerator();
int getDenominator( );
int getwholenumber( );
Rational operator + (Rational &r1);//addition operator overloaded
Rational operator - (Rational & r1);//subtraction operator overloaded
Rational operator * (Rational & r1);//multiply operator overloaded
Rational operator / (Rational & r1) ;//division operator overloaded
bool operator > (Rational & r1);//greater then operator overloaded
bool operator < (Rational & r1);//less the operator overloaded
bool operator >= (Rational & r1);//greater then equal to overloaded
bool operator <= ( Rational & r1);//less then equal to overloaded
bool operator != (Rational & r1);//not equality operator overloaded
Rational& operator ++ ();//increment operator overloaded
Rational& operator -- ();//decrement operator overloaded
bool operator == (Rational & r1);//equality operator overloaded
Rational & operator += (Rational & r1);//+=overloaded
friend istream & operator >> (istream & input, Rational & r1);//friend function for input
friend ostream & operator << (ostream & output, Rational & r1);//friend function for output
Rational &simplifyRation al();//simplification
~Rational();
};
/*************** *************** *************** *************** *************** ******
*************** *************co nstructor****** *************** *************** *************** **/
Rational::Ratio nal()
{
whl=0;
num=0;
denom=1;
}
/*************** **************g etters and setters******** *************** *************** **************/
void Rational::setNu merator(int n)
{
num = n;
}
void Rational::setDe nominator(int d)
{
denom = d;
}
void Rational::setwh olenumber(int w)
{
whl = w;
}
int Rational::getNu merator()
{
return num;
}
int Rational::getDe nominator()
{
return denom;
}
int Rational::getwh olenumber()
{
return whl;
}
Rational& Rational::setRa tional(int a,int b,int c)
{
this->whl=a;
this->num=b;
if (c==0)
this->denom=1;
else
{
this->denom=c;
this->num=(denom*whl )-num;
this->whl=num/denom;
this->denom=denom;
}
return *this;
}
/*************** *************ar ithmatic operators****** *************** *************** *************** */
Rational Rational:: operator + (Rational &r1)
{
Rational temp;
temp.num=(num*r 1.denom)+(denom *r1.num);
temp.denom=deno m*r1.denom;
temp.simplifyRa tional();
return temp;
}
Rational Rational:: operator - (Rational & r1)
{
Rational temp;
temp.num=(num*r 1.denom)-(denom*r1.num);
temp.denom=deno m*r1.denom;
temp.simplifyRa tional();
return temp;
}
Rational Rational::opera tor * (Rational & r1)
{
Rational temp;
temp.num=num*r1 .num;
temp.denom=deno m*r1.denom;
temp.simplifyRa tional();
return temp;
}
Rational Rational::opera tor / (Rational & r1)
{
Rational temp;
temp.num= num*r1.denom;
temp.denom=deno m*r1.num;
temp.simplifyRa tional();
return temp;
}
/*************** ***********comp arison operators****** *************** *************** *************** */
bool Rational:: operator > (Rational & r1)
{
if (num/denom > r1.num/r1.denom)
return true;
else
return false;
}
bool Rational:: operator < (Rational & r1)
{
if (num/denom < r1.num/r1.denom)
return true;
else
return false;
}
bool Rational:: operator >= (Rational & r1)
{
if ((num/denom > r1.num/r1.denom) || ((num==r1.num) && ( denom==r1.denom )))
return true;
else
return false;
}
bool Rational:: operator <= ( Rational & r1)
{
if (( num/denom < r1.num/r1.denom)|| ((num==r1.num)& &(denom==r1.den om)))
return true;
else
return false;
}
bool Rational:: operator != (Rational & r1)
{
if ((num!=r1.num)& &(denom!=r1.den om))
return true;
else
return false;
}
/*************** *************in crement & decrement operator******* *************** *************** ******/
Rational& Rational::opera tor ++ ()
{
this->num=num+deno m;
this->denom=denom;
return *this;
}
Rational& Rational:: operator -- ()
{
this->num=num-denom;
this->denom=denom;
return *this;
}
/*************** *************** assignment operator******* *************** *************** ******/
bool Rational:: operator == (Rational & r1)
{
if ((num==r1.num)& &(denom==r1.den om))
return true;
else
return false;
}
/*************** *************** *************** *************** *************** *************** ***/
Rational& Rational:: operator += (Rational & r1)
{
this->num=(num*r1.de nom)+(r1.num*de nom);
this->denom=denom*r1 .denom;
return *this;
}
/*************** *************** ***insertion & extraction operator******* *************** *************** ****/
istream & operator >> (istream & input, Rational & r1)
{
int num1,num2,num3;
input>>num1>>nu m2>>num3;
r1.setRational( num1,num2,num3) ;
return input;
}
ostream & operator << (ostream & output, Rational & r1)
{
r1.whl=r1.num/r1.denom;
r1.num=r1.num%r 1.denom;
output<<r1.whl< <" "<<r1.num<< "/"<<r1.denom ;
return output;
}
/*************** *************** *****simplify function******* *************** *************** **************/
Rational& Rational::simpl ifyRational()
{
int largest,gcd;
if(num>denom)
largest=num;
else
{
largest=denom;
for(int i=2;i<=largest; i++)
{
if ((num%i==0)&&(d enom%i==0))
gcd=i;
this->num=num/gcd;
this->denom=denom/gcd;
}
}
return *this;
}
/*************** *************** ****destructor* *************** *************** ***************/
Rational::~Rati onal()
{
}
/*************** *************** *************** *************** *************** *
*************** *************** *************** *************** *************** ***/
class RatUI{
Rational r1,r2;
public:
void showmenu()
{
char ch;
cout<<"Enter two rational no.s in (a b/c) format";
cin>>r1>>r2;
do{
clrscr();
cout<<"enter the choice to proceed";
cout<<"1: to sum two rational numbers";
cout<<"2: to subtract two rational numbers";
cout<<"3: to multiply two rational numbers";
cout<<"4: to divide two rational numbers";
cout<<"5: to compare two rational numbers by '>'";
cout<<"6: to compare two rational numbers by '<'";
cout<<"7: to compare two rational numbers by '<='";
cout<<"8: to compare two rational numbers by '>='";
cout<<"9: to copmare two rational numbers by '!='";
cout<<"10: to increment first rational number";
cout<<"11: to increment second rational number";
cout<<"12: to decrement first rational number";
cout<<"13: to decrement second rational number";
cout<<"14: to exit the menu";
option(ch);
}while(ch!='14' )
void option(char c)
{
switch(c)
{
case'1':cout<<r 1+r2;break;
case'2':cout<<r 1-r2;break;
case'3':cout<<r 1*r2;break;
case'4':cout<<r 1/r2;break;
case'5':cout<<r 1>r2;break;
case'6':cout<<r 1<r2;break;
case'7':cout<<r 1<=r2;break;
case'8':cout<<r 1>=r2;break;
case'9':cout<<r 1!=r2;break;
case'10':cout<< r1++;break;
case'11':cout<< r2++;break;
case'12':cout<< r1--;break;
case'13':cout<< r2--;break;
default:
break;
}
}
};
void main()//main driver program
{
RatUI r;
r.showmenu();
}
I am new to this site.can anyone help me in my program.It is of rational number class in c++ having operator overloading.It is not executing and i am not able to find mistakes.
My teacher told me that all functions in it should be cascadable.
plz plz point out my mistakes.I have to submit it on monday.
#include<iostre am.h>
#include<conio. h>
enum bool { false,true};
class Rational{
int num;//numerator
int denom;//denomenator
int whl;//whole number
public:
Rational();//constructor
Rational& setRational (int a,int b, int c);//setting the rational numbers
void setNumerator(in t);
void setDenominator( int);
void setwholenumber( int);
int getNumerator();
int getDenominator( );
int getwholenumber( );
Rational operator + (Rational &r1);//addition operator overloaded
Rational operator - (Rational & r1);//subtraction operator overloaded
Rational operator * (Rational & r1);//multiply operator overloaded
Rational operator / (Rational & r1) ;//division operator overloaded
bool operator > (Rational & r1);//greater then operator overloaded
bool operator < (Rational & r1);//less the operator overloaded
bool operator >= (Rational & r1);//greater then equal to overloaded
bool operator <= ( Rational & r1);//less then equal to overloaded
bool operator != (Rational & r1);//not equality operator overloaded
Rational& operator ++ ();//increment operator overloaded
Rational& operator -- ();//decrement operator overloaded
bool operator == (Rational & r1);//equality operator overloaded
Rational & operator += (Rational & r1);//+=overloaded
friend istream & operator >> (istream & input, Rational & r1);//friend function for input
friend ostream & operator << (ostream & output, Rational & r1);//friend function for output
Rational &simplifyRation al();//simplification
~Rational();
};
/*************** *************** *************** *************** *************** ******
*************** *************co nstructor****** *************** *************** *************** **/
Rational::Ratio nal()
{
whl=0;
num=0;
denom=1;
}
/*************** **************g etters and setters******** *************** *************** **************/
void Rational::setNu merator(int n)
{
num = n;
}
void Rational::setDe nominator(int d)
{
denom = d;
}
void Rational::setwh olenumber(int w)
{
whl = w;
}
int Rational::getNu merator()
{
return num;
}
int Rational::getDe nominator()
{
return denom;
}
int Rational::getwh olenumber()
{
return whl;
}
Rational& Rational::setRa tional(int a,int b,int c)
{
this->whl=a;
this->num=b;
if (c==0)
this->denom=1;
else
{
this->denom=c;
this->num=(denom*whl )-num;
this->whl=num/denom;
this->denom=denom;
}
return *this;
}
/*************** *************ar ithmatic operators****** *************** *************** *************** */
Rational Rational:: operator + (Rational &r1)
{
Rational temp;
temp.num=(num*r 1.denom)+(denom *r1.num);
temp.denom=deno m*r1.denom;
temp.simplifyRa tional();
return temp;
}
Rational Rational:: operator - (Rational & r1)
{
Rational temp;
temp.num=(num*r 1.denom)-(denom*r1.num);
temp.denom=deno m*r1.denom;
temp.simplifyRa tional();
return temp;
}
Rational Rational::opera tor * (Rational & r1)
{
Rational temp;
temp.num=num*r1 .num;
temp.denom=deno m*r1.denom;
temp.simplifyRa tional();
return temp;
}
Rational Rational::opera tor / (Rational & r1)
{
Rational temp;
temp.num= num*r1.denom;
temp.denom=deno m*r1.num;
temp.simplifyRa tional();
return temp;
}
/*************** ***********comp arison operators****** *************** *************** *************** */
bool Rational:: operator > (Rational & r1)
{
if (num/denom > r1.num/r1.denom)
return true;
else
return false;
}
bool Rational:: operator < (Rational & r1)
{
if (num/denom < r1.num/r1.denom)
return true;
else
return false;
}
bool Rational:: operator >= (Rational & r1)
{
if ((num/denom > r1.num/r1.denom) || ((num==r1.num) && ( denom==r1.denom )))
return true;
else
return false;
}
bool Rational:: operator <= ( Rational & r1)
{
if (( num/denom < r1.num/r1.denom)|| ((num==r1.num)& &(denom==r1.den om)))
return true;
else
return false;
}
bool Rational:: operator != (Rational & r1)
{
if ((num!=r1.num)& &(denom!=r1.den om))
return true;
else
return false;
}
/*************** *************in crement & decrement operator******* *************** *************** ******/
Rational& Rational::opera tor ++ ()
{
this->num=num+deno m;
this->denom=denom;
return *this;
}
Rational& Rational:: operator -- ()
{
this->num=num-denom;
this->denom=denom;
return *this;
}
/*************** *************** assignment operator******* *************** *************** ******/
bool Rational:: operator == (Rational & r1)
{
if ((num==r1.num)& &(denom==r1.den om))
return true;
else
return false;
}
/*************** *************** *************** *************** *************** *************** ***/
Rational& Rational:: operator += (Rational & r1)
{
this->num=(num*r1.de nom)+(r1.num*de nom);
this->denom=denom*r1 .denom;
return *this;
}
/*************** *************** ***insertion & extraction operator******* *************** *************** ****/
istream & operator >> (istream & input, Rational & r1)
{
int num1,num2,num3;
input>>num1>>nu m2>>num3;
r1.setRational( num1,num2,num3) ;
return input;
}
ostream & operator << (ostream & output, Rational & r1)
{
r1.whl=r1.num/r1.denom;
r1.num=r1.num%r 1.denom;
output<<r1.whl< <" "<<r1.num<< "/"<<r1.denom ;
return output;
}
/*************** *************** *****simplify function******* *************** *************** **************/
Rational& Rational::simpl ifyRational()
{
int largest,gcd;
if(num>denom)
largest=num;
else
{
largest=denom;
for(int i=2;i<=largest; i++)
{
if ((num%i==0)&&(d enom%i==0))
gcd=i;
this->num=num/gcd;
this->denom=denom/gcd;
}
}
return *this;
}
/*************** *************** ****destructor* *************** *************** ***************/
Rational::~Rati onal()
{
}
/*************** *************** *************** *************** *************** *
*************** *************** *************** *************** *************** ***/
class RatUI{
Rational r1,r2;
public:
void showmenu()
{
char ch;
cout<<"Enter two rational no.s in (a b/c) format";
cin>>r1>>r2;
do{
clrscr();
cout<<"enter the choice to proceed";
cout<<"1: to sum two rational numbers";
cout<<"2: to subtract two rational numbers";
cout<<"3: to multiply two rational numbers";
cout<<"4: to divide two rational numbers";
cout<<"5: to compare two rational numbers by '>'";
cout<<"6: to compare two rational numbers by '<'";
cout<<"7: to compare two rational numbers by '<='";
cout<<"8: to compare two rational numbers by '>='";
cout<<"9: to copmare two rational numbers by '!='";
cout<<"10: to increment first rational number";
cout<<"11: to increment second rational number";
cout<<"12: to decrement first rational number";
cout<<"13: to decrement second rational number";
cout<<"14: to exit the menu";
option(ch);
}while(ch!='14' )
void option(char c)
{
switch(c)
{
case'1':cout<<r 1+r2;break;
case'2':cout<<r 1-r2;break;
case'3':cout<<r 1*r2;break;
case'4':cout<<r 1/r2;break;
case'5':cout<<r 1>r2;break;
case'6':cout<<r 1<r2;break;
case'7':cout<<r 1<=r2;break;
case'8':cout<<r 1>=r2;break;
case'9':cout<<r 1!=r2;break;
case'10':cout<< r1++;break;
case'11':cout<< r2++;break;
case'12':cout<< r1--;break;
case'13':cout<< r2--;break;
default:
break;
}
}
};
void main()//main driver program
{
RatUI r;
r.showmenu();
}
Comment