Home Forums C Programming Object Oriented Programming Question Reply To: Object Oriented Programming Question

#3142
msaqib
Participant

Hello, you have done the following things wrong:
in the function definition ; is not allowed void rational::printrationald() also you will need to correct the variable name denominator I hope now your problem will be solved.

#include
using std::cout;
using std::endl;
class rational{
public:
rational (int=0, int=1);
rational addition (const rational &);
rational subtraction (const rational &);
rational multiplication (const rational &);
rational division (const rational &);
void printrational();
void printrationald();
private:
int numerator;
int denominator;
void reduction();
};
rational :: rational(int n, int d){
int numerstor=n;
int denominator =d;
}
rational rational ::addition (const rational &s){
rational w;
w.numerator= s.numerator*denominator;
w.numerator+=s.denominator*numerator;
w.denominator=s.denominator*denominator;
w.reduction();
return w;
}
rational rational:: subtraction(const rational &r){
rational w;
w.numerator= r.numerator*denominator;
w.numerator-=r.denominator*numerator;
w.denominator=r.denominator*denominator;
w.reduction();
return w;
}
rational rational ::multiplication (const rational &j)
{
rational w;
w.numerator= j.numerator*denominator;
w.denominator=j.denominator*denominator;
w.reduction();
return w;
}
rational rational ::division (const rational &k)
{
rational w;
w.numerator= k.numerator*denominator;
w.denominator=denominator*k.denominator;
w.reduction();
return w;
}
void rational::printrational()
{
cout<<numerator<<'t'<<denominator;
}
void rational::printrationald(); // you should write like that void rational::printrationald() without the ;
{
cout<<numerator'/'<<denomirator; //spellings for denomirator are wrong you should write denominator
}
void rational:: reduction()
{
int s;
s=numerator<denominator?numerator:denominator;
int x=0;
for (int l=2; l<=s;++l)
if (numerator % l == 0 && denominator % l)
x= l;
if (x!= 0)
{
int numinator = x;
int denominator = x;
}
} int main()
{
rational a(1, 7), f(9,2), h;
a.printrational();
cout<<"+";
f.printrational();
h= a.addition(f);
cout<<"=";
h.printrational();
cout<<'n';
h.printrational();
cout<<"=";
h.printrationald();
a.printrational();
f.printrational();
h=a.subtraction(f);
cout<<"=";
h.printrational();
cout<<'n';
h.printrational();
cout<<"=";
h.printrationald();
a.printrational();
cout<<"h";
f.printrational();
h=a.multiplication(f);
cout<<"=";
h.printrational();
cout<<'n';
h.printrational();
cout<<'=';
h.printrationald();
a.printrational();
f.printrational();
h=a.division(f);
cout<<"=";
h.printrational();
h.printrational();
cout<<"=";
h.printrationald();
cout<<endl;
return 0;
}