insertion and extraction operators

I have to include friend extraction and insertion operators for my Fractions class. The insertion operator is supposed to allow the object to be output and appear in the form num/denom using a cout statement. The extraction operator should allow fractions to be input in the form a/b and assigned to the fraction object. I am having difficulty figuring out how to go about achieving these two things. Here is my code so far.
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<iomanip>
using namespace std;

class Fractions
{
    private:
        int num,denom;
    public:
        Fractions();
        Fractions(int);
        Fractions(int,int);
        int numerator();
        int denominator();
        void display();
        Friend istream& operator>>(istream&, Fractions&);
        Friend ostream& operator<<(ostream&, Fractions&);
        
        
    int gcd(int n, int d)
    {
        int remainder;
        while (d != 0)
        {
            remainder = n % d;
            n = d;
            d = remainder;
        }
        return n;
    }
};

Fractions::Fractions()
{
    num=1, denom=1;
}

Fractions::Fractions(int n)
{
    num=n, denom=1;
}

Fractions::Fractions(int n, int d)
{
    num = n;
        if (d==0) 
        {
            cout << "Error: DIVISON BY ZERO!" << endl;
            exit(1); // will terminate the program if user enters 0 as denominator
        }
        else
            denom = d;
}

int Fractions::numerator()
{
    return num;
}

int Fractions::denominator()
{
    return denom;
}

void Fractions::display()
    {
        cout << num << "/" << denom;
    }    
    
Fractions operator+(Fractions a, Fractions b)
    {
        int n = a.numerator() * b.denominator() + a.denominator() * b.numerator();
        int d = a.denominator() * b.denominator();
        Fractions c(n/a.gcd(n,d),d/b.gcd(n,d)); // a/b + c/d = (a * d + b * c) / (b * d)
        return c;        
    }
    
Fractions operator-(Fractions a, Fractions b)
    {
        int n = a.numerator() * b.denominator() - a.denominator() * b.numerator();
        int d = a.denominator() * b.denominator();
        Fractions c(n/a.gcd(n,d),d/b.gcd(n,d)); // a/b - c/d = (a * d - b * c) / (b * d)
        return c;        
    }
    
Fractions operator*(Fractions a, Fractions b)
    {
        int n = a.numerator() * b.numerator();
        int d = a.denominator() * b.denominator();
        Fractions c(n/a.gcd(n,d),d/b.gcd(n,d)); // a/b * c/d = (a * c) / (b * d)
        return c;        
    }
    
Fractions operator/(Fractions a, Fractions b)
    {
        int n = a.numerator() * b.denominator();
        int d = a.denominator() * b.numerator();
        Fractions c(n/a.gcd(n,d),d/b.gcd(n,d)); // (a/b) / (c/d) = (a * d) / (b * c)
        return c;        
    }

istream& operator>>(istream& in, Fractions& f)
{
	int n,d;
	char x;
	in >> n >> x >> d;
	Fractions c(n,d);
	f=c;
	return in;
}

ostream& operator<<(ostream& out, Fractions f)
{
    if (f.denominator() == 1)
        {
            out << f.numerator();
        }
    else
    {
        out << f.numerator() << "/" << f.denominator();
    }
	return out;
}


//Main

int main()
{
    Fractions a; // a = 1/1
    Fractions b(5); // b = 5/1
    Fractions c(6,8); // c = 6/8
    
    cout << "Display fractions (1 will be shown in the denominator): " << endl;
    a.display(); //tests display function, does not disregard 1 in the numerator
    cout << endl;
    b.display();
    cout << endl;
    c.display();
    cout << endl;

    cout << "Display fractions properly (1 in the denominator will be omitted): " << endl;
    cout << "a=" << a << ", b=" << b << ", c=" << c << endl; //fraction 2/1 will display simply as 2, so should display a=1, b=5, c=6/8

    cout << "The numerator of c is " << c.numerator()
         << " and the denominator is " << c.denominator() << endl << endl; //should show 6 then 8           
         
    Fractions d;
    cout << "Enter a fraction: ";
    cin >> d; // tests >> operator

    cout << "d=" << d << endl; //should display the fraction user just entered

    cout << "c+d=" << c+d << endl; //display reduced fractions as a result
    cout << "c-d=" << c-d << endl; //display reduced fractions as a result
    cout << "c*d=" << c*d << endl; //display reduced fractions as a result
    cout << "c/d=" << c/d << endl; //display reduced fractions as a result

    system("PAUSE");
    return 0;
}








YOur functions looks fine. You just need to define operator= for your class.
And how would I go about doing that?
Topic archived. No new replies allowed.