Overloading operators help

I need help understanding how to overload my arithmetic functions in my code, im not asking for it to be done, but i need help. here is what I have:


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
//Fraction.cpp
#include <iostream>
#include <iomanip>
#include "fraction.h"

using namespace std;

int gcd(int u, int v)

int main()
{
 int anum;
 int bden;
 int cnum;
 int dden;
 int u;
 int v;
 char choice;
fraction add(fraction f2);
{
 do
 {
  cout << "a\tadd\n";
  cout << "s\tsub\n";
  cout << "m\tmult\n";
  cout << "d\tdiv\n";
  cout << "e\texit\n";

  cin >> choice;
  cin.ignore();

  switch (choice)
  {
   case 'A':
   case 'a':
    cout << "enter the left numerator: ";
    cin >> anum;
    cout << "enter the left denominator: ";
    cin >> bden;
    cout << "enter the right numerator: ";
    cin >> cnum;
    cout << "enter the right denominator: ";
    cin >> dden;
    cout << ((anum * dden) + (bden * cnum)) / (bden * dden) << endl;
    break;

    case 'S':
   case 's':
    cout << "enter the left numerator: ";
    cin >> anum;
    cout << "enter the left denominator: ";
    cin >> bden;
    cout << "enter the right numerator: ";
    cin >> cnum;
    cout << "enter the right denominator: ";
    cin >> dden;
    cout << ((anum * dden) - (bden * cnum)) / (bden * dden) << endl;
    break;

    case 'M':
   case 'm':
    cout << "enter the left numerator: ";
    cin >> anum;
    cout << "enter the left denominator: ";
    cin >> bden;
    cout << "enter the right numerator: ";
    cin >> cnum;
    cout << "enter the right denominator: ";
    cin >> dden;
    cout << (anum * cnum) / (bden * dden) << endl;
    break;

    case 'D':
   case 'd':
    cout << "enter the left numerator: ";
    cin >> anum;
    cout << "enter the left denominator: ";
    cin >> bden;
    cout << "enter the right numerator: ";
    cin >> cnum;
    cout << "enter the right denominator: ";
    cin >> dden;
    cout << (anum * dden) / (bden * cnum) << endl;
    break;

    case 'E':
    case 'e':
     break;

    default:
     cerr << "unrecognized choice: " << choice << endl;

  }
 } while(choice != 'e' && choice != 'E');


 int gcd (int u, int v);

 u = (u < 0) ? -u : u;
 v = (v < 0) ? -v : v;

 while (u > 0)
  if (u < v)
  {
   int t = u;
   u = v;
   v = t;
  }
  u -= v;
 }
 
 return v;

 
}

void print()
 {
  int numerator;
  int denominator;
  cout << numerator << '/' << denominator;
}

void read()
{
 int numerator;
 int denominator;
 cout << "Please enter the numerator: ";
 cin >> numerator;
 cout << "Please enter the denominator: ";
 cin >> denominator;
}


Here is the header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

class fraction
{
private:
 int numerator;
 int denominator;

public:
 fraction() : numerator(), denominator(){}
  void print();
  fraction add(fraction f2);
  void read();
  
 

};
You'll want to use the operator keyword, and its usage is almost exactly the same:

[return] operator [symbol] ([argument list])

1
2
3
4
5
6

Fraction& operator + (fraction& f2)
{
   //do stuff here
};


try this:

http://www.cplusplus.com/doc/tutorial/classes2/
Last edited on
Topic archived. No new replies allowed.