header files?

Im wondering what I need to do to build a header file from this. I know that I need the basics of the class in there with the include and end tags but I'm just not understanding why things aren't working.

This is the header file:
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
  #ifndef RATIONAL_H
#define RATIONAL_H

#include<iostream>
using namespace std;

/**
 * @brief  Instances of class %Rational represent rational numbers.
 */
class Rational{
public: //-------------------------------------------------------------------------------------------
    
    /**
     * @brief  Construct an object representing an %Integer &amp; having initial value 0
     */
    Rational(int a, int b);
    
    Rational();
    
    int getNumerator();
    
    void setNumerator(int n);
    
    int getDenominator();
    
    void setDenominator(int n);
    
    friend Rational operator+  (Rational &a, Rational &b);
    
    friend Rational operator-  (Rational &a, Rational &b);
    
    friend Rational operator*  (Rational &a, Rational &b);
    
    friend Rational operator/  (Rational &a, Rational &b);
    
    friend bool operator== (Rational &a, Rational &b);
    
    friend bool operator<= (Rational &a, Rational &b);
    
    friend bool operator>= (Rational &a, Rational &b);
    
    friend bool operator<  (Rational &a, Rational &b);
    
    friend bool operator>  (Rational &a, Rational &b);
    
    friend ostream& operator<< (ostream& fout, const Rational& r);
    
    friend istream& operator>> (istream& fin, Rational& r);
    
    
private: //---------------------------------------------------------------------------------------------
    
    int numerator;
    int denominator;
}; // End Rational Class

#endif



here's the .cpp file.
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
#include <iostream>
#import "Rational.h"
using namespace std;

int getNumerator(){
    return numerator;
} //end getNumerator

void setNumerator(int n){
    numerator = n;
} //end setNumerator

int getDenominator(){
    return denominator;
} //end getDenominator

void setDenominator(int n){
    denominator = n;
} //end setDenominator

friend Rational operator+ (Rational &a, Rational &b){
    //a/b + c/d = (a * d + b * c) / (b * d)
    Rational RationalSum;
    RationalSum.setNumerator(a.getNumerator() * b.getDenominator() +
                             a.getDenominator() * b.getNumerato());
    RationalSum.setDenominator (a.getDenominator() * b.getDenominator());
    return RationalSum;
} // end operator+

friend Rational operator- (Rational &a, Rational &b){
    //a/b - c/d = (a * d - b * c) / (b * d)
    Rational RationalSubt;
    RationalSubt.setNumerator(a.getNumerator() * b.getDenominator() -
                              a.getDenominator() * b.getNumerator());
    RationalSubt.setDenominator (a.getDenominator() * b.getDenominator());
    return RationalSubt;
} // end operator-

friend Rational operator* (Rational &a, Rational &b){
    //(a/b) * (c/d) = (a * c) / (b * d)
    Rational RationalMult;
    RationalMult.setNumerator(a.getNumerator() * b.getNumerator());
    RationalMult.setDenominator (a.getDenominator() * b.getDenominator());
    return RationalMult;
} // end operator*

friend Rational operator/ (Rational &a, Rational &b){
    //(a/b) / (c/d) = (a * d) / (c * b)
    Rational RationalDiv;
    RationalDiv.setNumerator(a.getNumerator() * b.getDenominator());
    RationalDiv.setDenominator(a.getDenominator() * b.getNumerator());
    return RationalDiv;
} //end operator/

friend bool operator== (Rational &a, Rational &b){
    //(a/b) == (c/d) means (a * d) == (c * b)
    Rational RationalEqu;
    RationalEqu.setNumerator(a.getNumerator() * b.getDenominator());
    RationalEqu.setDenominator(b.getNumerator() * a.getDenominator());
    return RationalEqu;
} //end operator==

friend bool operator< (Rational &a, Rational &b){
    //(a/b) < (c/d) means (a * d) < (c * b)
    Rational RationalLess;
    RationalLess.setNumerator(a.getNumerator() * b.getDenominator());
    RationalLess.setDenominator(b.getNumerator() * a.getDenominator());
    return RationalLess;
} // end operator<

friend bool operator<= (Rational &a, Rational &b){
    Rational RationalLorE;
    RationalLorE.setNumerator(a.getNumerator() * b.getDenominator());
    RationalLorE.setDenominator(b.getNumerator() * a.getDenominator());
    return RationalLorE;
} // end operator<=

friend bool operator> (Rational &a, Rational &b){
    //(a/b) > (c/d) means (a * d) > (c * b)
    Rational RationalGreat;
    RationalGreat.setNumerator(a.getNumerator() * b.getDenominator());
    RationalGreat.setDenominator(b.getNumerator() * a.getDenominator());
    return RationalGreat;
} // end operator>

friend bool operator>= (Rational &a, Rational &b){
    Rational RationalGorE;
    RationalGorE.setNumerator(a.getNumerator() * b.getDenominator());
    RationalGorE.setDenominator(b.getNumerator() * a.getDenominator());
    return RationalGorE;
} // end operator>=

ostream& operator<<(ostream& fout, const Rational& r){
    if (r.denominator== 0)
        out << "undefined" ;
    else
        out << r.numerator << "/" << r.denominator;
    return fout;
} //end ostream

istream& operator>>(istream& fin, Rational& r){
    in >> r.numerator >> r.denominator;
    return fin;
} // end istream

Rational::Rational(int a, int b){
    numerator = a;
    denominator = b;
} //end Rational

Rational::Rational(){
    numerator = 5;
    denominator = 10;
} //end Rational

Rational::Rational(){
    num = 0;
    dem = 1;
} // end Rational

Rational::Rational(int whole){
    num = whole;
    dem = 1;
} // end Rational

int main(){
    //Rational rational;
    //Rational a;
    //Rational b(1,2);
    //Rational bad(1,0);
    //Rational c(10);
    
    //cout << "Enter your number: ";
    //cin >> rational;
    
    //cout << "You entered the number " << rational << ".\n";
    
    return 0;
} // end main function 
in your cpp file you need to add Rational:: to all your methods that you declared in your h file.

for example

1
2
3
int getNumerator(){
    return numerator;
} //end getNumerator 


should be


1
2
3
int  Rational::getNumerator(){
    return numerator;
} //end getNumerator 



would I do that with the friend members also?
No, because friends are not member functions of your class - the reason why people make operator overload functions friends is so that they can see the private data members of your class instance. Hope that makes sense.

You also have ambiguous functions, for example:
1
2
3
4
5
6
7
8
9
Rational::Rational(){
    numerator = 5;
    denominator = 10;
} //end Rational

Rational::Rational(){
    num = 0;
    dem = 1;
} // end Rational 

Both of these have the same signature, choose only one.
Last edited on
yeah, I understand
So would the friend functions even be a part of my class?
Topic archived. No new replies allowed.