overloaded << operator

Im having a problem passing the correct value to my overloaded << operator. Its displaying some random garbage number. its lines 50 through 55. I tried looking at older posts to answer my question but i could not find a working example of how this works any help would be appreciated thank you

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
#include <iostream>
using namespace std;

class Rational
{
int num;
int den;

public:
       Rational(); //Default set to 0/1       
       Rational(int num);//set to whole number over 1 
       Rational(int num, int den); //set to any values               
      
       void setnum(int);
       void setden(int);
       void simplify(int num, int den); //simplify rational number 
       void displayRational(); // displays rational number
       
       friend istream& operator>>(istream& ins, Rational& ratnum); //input opperator overload 
       friend ostream& operator>>(ostream& outs, const Rational& ratnum); //output operator overload
       
       friend Rational operator+(const Rational& ratnum1, const Rational& ratnum2); //addition
       friend Rational operator-(const Rational& ratnum1, const Rational& ratnum2); //subtraction
       friend Rational operator*(const Rational& ratnum1, const Rational& ratnum2); //multiply
       friend Rational operator/(const Rational& ratnum1, const Rational& ratnum2); //divide 
       
       friend bool operator<(const Rational & ratnum1, const Rational & ratnum2); //less than
       friend bool operator>(const Rational & ratnum1, const Rational & ratnum2); //greater than
       friend bool operator<=(const Rational & ratnum1, const Rational & ratnum2); //less than or equal to
       friend bool operator>=(const Rational & ratnum1, const Rational & ratnum2); //greater than or equal to 
       friend bool operator==(const Rational & ratnum1, const Rational & ratnum2); //equal to 
};

Rational::Rational(): num(0), den(1)
{}//default constructor

Rational::Rational(int num): num(num), den(1)//sets numerator to whole number
{}

Rational::Rational(int num, int den): num(num), den(den)//sets to any value
{}

//simplify 

void Rational::displayRational()// displays a rational number
{
     cout << num <<"/"<< den << endl;
}
//input
ostream& operator << (ostream& outs, const Rational& RationalNmber) //output
{
  int num, den; 
    
  outs << num << "/" << den ;
  return outs;   
}   

      
       
int main()
{      
 Rational test1, test2(10), test3(1, 2);
 
 test1.displayRational();
 test2.displayRational();
 test3.displayRational();

  
 cout << "\nTest1 equals " << test1;
 cout << "\nTest2 equals " << test2;
 cout << "\nTest3 equals " << test3;
 
 system ("pause");
 return 0;
}
what are the values for num and den? I did not look at your code but they are not defined.
I had the idea that the constructors would set the values of num and den from the test objects. And I was hoping for the same output as the displayRational function from the overloaded << operator
Last edited on
1
2
3
4
5
6
7
ostream& operator << (ostream& outs, const Rational& RationalNmber) //output
{
  int num, den; //uninitialized data
    
  outs << num << "/" << den ; // but you use it as if it were initialized
  return outs;   
}


Maybe you meant something like:
1
2
3
4
5
ostream& operator << (ostream& outs, const Rational& RationalNmber) //output
{
  outs << RationalNmber.num << "/" << RationalNmber.den ;
  return outs;   
}
i tried something like that and i get an error that the variables are private is there a way around this while keeping the the variable private. Do i need a set function or something?
You forgot to declare ostream& operator << (ostream& outs, const Rational& RationalNmber) as a friend of your class.
its declared as a friend on line 20 but i forgot i changed the parameter from ratnum to rationalNumber so thats why it didnt work made the one change and added outs << RationalNmber.num << "/" << RationalNmber.den ; to my function and now it works as intended. Thank you Danny for pointing me in the right direction.
Last edited on
Why do you have them all declared as friends? Only a few of them need to be.
really? in my lame text book it made it sound like any overloaded operator that accesses a private variable needs to be a friend function. Is there an easier way?
What giblit means is that you only ever need to implement operator== and operator<, since all other operatoes can be derived from those two.
http://www.cplusplus.com/reference/utility/rel_ops/
Last edited on
Yes sorry I should of been more clear I meant what LB said and also that you shouldn't need friend on your operator+,-,*,/,%

The major ones I can think of where you need friend is for streams with operator ex: ostream operator<< or istream operator>>
and comparison operators like == or <

Also you want to overload the compound operators in your class then use them for the others.

Ex you overload == and < then for > you would put something like return( !(object < object ) && !( object == object ) );

Same goes for the rest of comparison just do some tweaking.

Here is an example

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
#include <iostream>

class num
{
	public:
		num( int value );
		num operator+( const num &rhs );
		friend std::ostream &operator<<( std::ostream &stm , const num &rhs );
	private:
		int value;
};

num::num( int value ) : value( value ){}
num num::operator+( const num &rhs)
{
	num result = *this;
	result.value += rhs.value;
	return( result );
}
std::ostream &operator<<( std::ostream &stm , const num &rhs )
{
	return( stm << rhs.value );
}

int main()
{
	num n1( 10 ) , n2( 20 );
	std::cout << ( n1 + n2 ) << std::endl;
}
30
friend is also convenient for declaring global functions/operators within the scope of templates classes, as it makes the syntax much more concise, even in cases where no access to private or protected members is needed.
Thank you LB and giblit that makes it a lot easier to understand and will make my program easier to write. Why doesn't my text book say these things?
Topic archived. No new replies allowed.