Correct please

Here is the Problem:

Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers.
For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. Represent rational numbers as two values of type int,
one for the numerator and one for the denominator. Call the class Rational. Implement the following functions for this class:

1. Include a constructor with two arguments that can be used to set the member variables of an object
to any legitimate values.
2. Also include a constructor that has only a single parameter of type int; define the constructor such that an object is
initialized with denominator always equal to 1.
3. Also include a default constructor that initializes an object to 0 (that is, to 0/1).
4. Overload all of the following operators so that they correctly apply to the type Rational:
==, <, >, +, -, *, <<, >>. Make all the overloaded operators "friends" of the class.
5. Whenever possible, make these functions efficient and secure.

Remember to make all member variables "private".
When implementing ==, note that the rational number 2 is equal to 2/1 and 4/2 both. Similarly, implement all other operators
properly. You should also check for wrong inputs, say 1/0.

int main()
{
Rational x; //this will call default constructor to initialize x to 0/1
Rational y(3); //this should initialize y to 3/1
Rational z(2, 3); //this will be a rational number 2/3
cout<<x; //it prints rational number in the form numerator/denominator
cout<<y;
cout<<z;
if(x<y) cout<<"x is smaller than y\n";
else if(x==y) cout<<"x is equal to y\n";
else if(x>y) cout<<"x is greater than y\n";
Rational a = y+z;
cout<<"y+z="<<a;
Rational b = y-z;
cout<<"y-z="<<b;
Rational c = y*z;
cout<<"y*z="<<c;
cout<<"Enter a rational number:";
cin>>x;
cout<<"You entered "<<x;
return 0;
}


and this is my current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
using namespace std;

class Rational
{
	friend bool operator==(const Rational& p, const Rational& q);
	friend Rational operator+(const Rational& p, const Rational& q);
	friend Rational operator*(const Rational& p, const Rational& q);
	friend Rational operator-(const Rational& p, const Rational& q);
	friend bool operator<(const Rational& p, const Rational& q);
	friend bool operator>(const Rational& p, const Rational& q);
	friend ostream& operator<<(ostream& os, const Rational& p);
	friend istream& operator>>(istream& is, Rational& p);
private:
	int x;
	int y;
public:
	Rational();
	Rational(int xx, int yy);
	Rational(int yyy);
};
Rational::Rational()
{
	x=0;
	y=1;
}
Rational::Rational(int xx, int yy)
{	
	if(yy==0)
	{
		cout<<"Illegal Input"<<endl;
		yy=1;
	}
	else	
	{	x=xx;
		y=yy;
	}
}
Rational::Rational(int yyy)
{
	if(yyy=0)
	{
		cout<<"Illegal Input"<<endl;
		yyy=1;
	}
	y=yyy;
}

bool operator==(const Rational& p, const Rational& q)
{
	if((p.x==q.x)&&(p.y==q.y))
		return true;
	else
		return false;
}
Rational operator+(const Rational& p, const Rational& q)
{
	Rational z;
		z.x=p.x+q.x;
		z.y=p.y+q.y;
	return z;
}
Rational operator*(const Rational& p, const Rational& q)
{
	Rational z(p.x*q.x, p.y*q.y);
	return z;
}
	Rational operator-(const Rational& p, const Rational& q)
	{
		Rational z(p.x-q.x, p.y-q.y);
		return z;
	}
	bool operator<(const Rational& p, const Rational& q)
	{
		if(((p.x)<(q.x))&&((p.y)<(q.y)))
			return true;
		else 
			return false;
	}
	bool operator>(const Rational& p, const Rational& q)
	{
		if(((p.x)>(q.x))&&((p.y)>(q.y)))
			return true;
		else 
			return false;
	}
	ostream& operator<<(ostream& os, const Rational& p)
	{
		os<<p.x<<"/"<<p.y<<endl;
		return os;
	}
	istream& operator>>(istream& is, Rational& p)
	{
		is>>p.x>>p.y;
		return is;
	}
int main()
{
	Rational x; //this will call default constructor to initialize x to 0/1
	Rational y(3); //this should initialize y to 3/1
	Rational z(2, 3); //this will be a rational number 2/3
	cout<<x; //it prints rational number in the form numerator/denominator
	cout<<y;
	cout<<z;
	if(x<y) cout<<"x is smaller than y\n";
	else if(x==y) cout<<"x is equal to y\n";
	else if(x>y) cout<<"x is greater than y\n";
	Rational a = y+z;
	cout<<"y+z="<<a;
	Rational b = y-z;
	cout<<"y-z="<<b;
	Rational c = y*z;
	cout<<"y*z="<<c;
	cout<<"Enter a rational number:";
	cin>>x;
	cout<<"You entered "<<x;
	system("pause");
	return 0;
}
Last edited on
Hi ,
I think that this part is wrong

1
2
3
4
5
6
7
8
9
10
11
12
Rational::Rational(int xx, int yy)
{	
	if(yy=0)
	{
		cout<<"Illegal Input"<<endl;
		x=0;y=1;
	}
	else 
	{	x=xx;
		y=yy;
	}
}


I should be
1
2
3
4
5
6
7
8
9
10
11
12
Rational::Rational(int xx, int yy)
{	
	if(yy==0)
	{
		cout<<"Illegal Input"<<endl;
		x=0;y=1;
	}
	else 
	{	x=xx;
		y=yy;
	}
}
Hey, thx for the reply.
No it is not, here is the instructor instruction:


1. Include a constructor with two arguments that can be used to set the member variables of an object
to any legitimate values.
2. Also include a constructor that has only a single parameter of type int; define the constructor such that an object is
initialized with denominator always equal to 1.
hi
in your Rational(int xx, int yy) constructor
why do you set x = 0, when the value of yy == 0 is ?

I think you should set y = 1 ; and x = xx

1
2
3
4
5
6
7
8
9
10
Rational::Rational(int xx, int yy)
{	
	if(yy==0)
	{
		cout<<"Illegal Input"<<endl;
		y=1;
	}
		x=xx;
	
}
You should be more clear about what the error is.
As pointed by bluecoder if(yy=0) //line 29
Last edited on
Thx but i tried that and it did not work. try to run it, it will give u some garbage values :S
1
2
3
4
5
6
7
8
9
10
11
12
	Rational y(3); //this should initialize y to 3/1  (liar)

Rational::Rational(int yyy) //you aren't touching x here (garbage). But you're assigning y
{	
	if(yyy==0)
	{
		cout<<"Illegal Input"<<endl;
		yyy=1;
	}
	else
		y=yyy;
}
Check out your math. That is not how you add/substract/compare rationals.

Also
1
2
3
4
5
6
istream& operator>>(istream& is, Rational& p)
	{
		is>>p.x>>p.y; //so, separated by a space...
//You aren't validating the denominator
		return is;
	}
and
1
2
3
4
5
6
7
8
9
10
11

Rational::Rational(int yyy) //you aren't touching x here (garbage). But you're assigning y
{	
	if(yyy==0)
	{
		cout<<"Illegal Input"<<endl;
		yyy=1;
	}
	//else you don't need the else here
	y=yyy;
}
still it keeps giving me some garbage values, i tried everything :S
plz post your input data, and what you get as output, post them all with your current code
HELP PLEASE!!!
Last edited on
@Eyad

Could you please post your input data, and what you get as output,
and what you expect your code to do, what output do you expect ?
and please post your current code ?
Last edited on
Topic archived. No new replies allowed.