c++ beginner problem

Hi,
I am c++ beginner and i need some help with a problem.

Made a Rational class who allows you to work with rational numbers. Class Construct will have two arguments : numerator and denominator. You must made a copy constructor.
It will provide member functions :
Access to the numerator, denominator that rational number.
redefining operators + =, - =, * =, / = for addition, subtraction, multiplication and division with another rational number rational number given as an argument.
It will redefine operator >> as a friend function to read a rational number from the keypad.

It will provide member functions:
test of equality of two rational numbers (redefining operator ==).
writing a rational number to stdout (redefining operator <<).
redefining operators +, -, *, / to allow operation with two arguments rational numbers.
redefining the assignment operator.

Write a main function that reads n rational fractions and calculates their sum.


#include <iostream>
#include <iomanip>
#include <conio.h>
#include <stdlib.h>

using namespace std;

class Rational
{
int numarator;
int numitor;
public:
int cmmdc(int a, int b);
Rational(int n1 = 0, int n2 = 1)
{
numarator = n1;
numitor = n2;
}
Rational(Rational &r)
{
numarator = r.sus();
numitor = r.jos();
}
int sus() const
{
return numarator;
}
int jos() const
{
return numitor;
}
void setsus(int x)
{
numarator = x;
}
void setjos(int y)
{
numitor = y;
}
Rational &operator += (const Rational &r);
Rational &operator -= (const Rational &r);
Rational &operator *= (const Rational &r);
Rational &operator /= (const Rational &r);
friend istream &operator>> (istream &input, Rational &r);
friend ostream &operator<< (ostream &output, const Rational &r);
friend int operator== (const Rational &r1, const Rational &r2);
friend Rational operator+ (const Rational &r1, const Rational &r2);
friend Rational operator- (const Rational &r1, const Rational &r2);
friend Rational operator* (const Rational &r1, const Rational &r2);
friend Rational operator/ (const Rational &r1, const Rational &r2);
friend void simplifica(Rational &r);
};

Rational &Rational::operator += (const Rational &r)
{
int t = numarator * r.jos() + numitor * r.sus();
numitor = numitor * r.jos();
numarator = t;
return *this;
}

Rational &Rational::operator-=(const Rational &r)
{
int t = numarator * r.jos() - numitor * r.sus();
numitor = numitor * r.jos();
numarator = t;
return *this;
}

Rational &Rational::operator*=(const Rational &r)
{
numarator *= r.sus();
numitor *= r.jos();
return *this;
}

Rational &Rational::operator/=(const Rational &r)
{
numarator *= r.sus();
numitor *= r.jos();
return *this;
}

int Rational::cmmdc(int a, int b)
{
int r;
do
{
r = a % b;
a = b;
b = r;
}while(r);
return a;
}

istream &operator>>(istream &input, Rational &r)
{
int x, y;
input >> x >> y;
r.setsus(x);
r.setjos(y);
return input;
}

ostream &operator<<(ostream &output, const Rational &r)
{
output<<r.sus()<<" / "<< r.jos();
return output;
}

int operator==(Rational &r1, Rational &r2)
{
simplifica(r1);
simplifica(r2);
return r1.sus()==r2.sus() && r1.jos()==r2.jos();
}

Rational operator+(const Rational &r1, const Rational &r2)
{
Rational r(r1);
r += r2;
return r;
}

Rational operator-(const Rational &r1, const Rational &r2)
{
Rational r(r1);
r -= r2;
return r;
}

Rational operator*(const Rational &r1, const Rational &r2)
{
Rational r(r1);
r *= r2;
return r;
}

Rational operator/(const Rational &r1, const Rational &r2)
{
Rational r(r1);
r /= r2;
return r;
}

void simplifica(Rational &r)
{
int s = r.sus();
int j = r.jos();
int c = r.cmmdc(s, j);
s /= c;
j /= c;
r.setsus(s);
r.setjos(j);
}

int main()
{
Rational suma, t;
int n;
cout <<"\n Dati numarul de termeni : ";
cin >> n;
cout << "\n Introduceti fractiile, cate una pe linie : "<<endl;
for(int i=0; i<n; i++)
{
cin << t;
simplifica(t);
suma += t;
simplifica(suma);
}cout << "\n suma = " << suma <<endl;
return 0;
}
1) Please use code tags to make your code readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) What exactly is your problem?
The code is pretty good. After trying to compile it I found a couple of minor problems.

The argument to the copy constructor should be const:
Rational(const Rational & r)

In main(), cin << t should be cin >> t

Two other comments:
simplifica should be a method of Rational:
1
2
3
4
5
6
void Rational::simplifica()
{
    int c  cmmdc(numarator, numitor);
    numarator /= c;
    numitor /= c;
}


Also notice that cmmdc() doesn't access any members of Rational, so it doesn't need to be a member, or at least it could be a static member. Since it isn't really needed as part of the interface, I'd remove it from the Rational class completely and make it a static function instead:
static int cmmdc(int a, int b)


Thank you dhayden.
I know MikeBoy, you right, just i forgot.
Thank you again.
Topic archived. No new replies allowed.