I need some help.. I can't get it to work. The / operator is giving error also the friend ostream.
#include <iostream>
#include <string>
#include <iomanip>
#include "rational.h"
using namespace std;
Rational::Rational()
{
Numerator=0;
Denominator=1;
}
Rational::Rational (int n, int d )
{
Numerator=n;
Denominator=d;
if(Denominator == 0) Denominator =1;
if(Denominator < 0)
{
Numerator *= -1;
Denominator *= -1;
}
}
Rational operator+(const Rational&rhs)
{
int Numerator;
int Denominator;
return Rational((Numerator * rhs.get_Denominator() + Denominator*rhs.get_Numerator()), (Denominator * rhs.get_Denominator()));
}
Rational operator-(const Rational &rhs)
{
return operator+(Rational(-1*rhs.get_Numerator(),rhs.get_Denominator()));
}
Rational operator*(const Rational&rhs)
{
int Numerator;
int Denominator;
return Rational(Numerator * rhs.get_Numerator(), Denominator * rhs.get_Denominator());
}
ostream &operator<<( ostream & out, const Rational &r )
{
out << r.get_Numerator() << "/" << r.get_Denominator();
return out;
}
---------------------------------------------------------
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
friend ostream& operator<<( ostream&, const Rational & );
public:
Rational();
Rational(int);
Rational(int , int );
int get_Numerator()const{return Numerator;}
int get_Denominator()const{return Denominator;}
Rational operator +(const Rational &);
Rational operator -(const Rational &);
Rational operator *(const Rational &);
Rational operator /(const Rational &);
private:
int Numerator;
int Denominator;
};
#endif
-------------------------------------------------------
#include <iostream>
#include "rational.h"
using namespace std;
int main ()
{
Rational r1,r2(3),r3(11,3),tmp;
Rational s(2,4);
Rational c;
cout << "The default rational # is " << c << endl;
cout << r1 << " + " << r2 << " = " << r1+r2 << endl;
cout << r2 << " - " << r3 << " = " << r2-r3 << endl;
cout << r2 << " * " << r3 << " = " << r2*r3 << endl;
cout << r1 << " / " << r3 << " = " << r1/r3 << endl;
return 0;
cin.ignore();
cin.get();
}
Last edited on
1- Use [code][ /code wrapper.
2- Use indents.
3- Don't throw the code at our face like that. Explain where exactly your problem is.