How to Implement these operator overloading into a class?

Hello. I have this code I wrote myself where I overloaded operators to add, subtract, multiply and divide fractions. I want to implement the overloading into a class. Can anyone help me out? Thanks.
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
#include <iostream>
#include <string>
using namespace std;

class calculator
{
private:
	int num;

public:
	calculator() : num(0)
	{}

	void getdata()
	{
		cout << "Please enter a number: ";
		cout << endl;
		cin >> num;
	}
	void showdata()
	{
		cout << num << endl;
		cout << endl;
	}
	calculator operator + (calculator) const;
	calculator operator - (calculator) const;
	calculator operator * (calculator) const;
	calculator operator / (calculator) const;
};


calculator calculator::operator + (calculator arg2) const
{
	calculator temp;
	temp.num = num + arg2.num;
	return temp;
}

calculator calculator::operator - (calculator arg2) const
{
	calculator temp;
	temp.num = num - arg2.num;
	return temp;
}

calculator calculator::operator * (calculator arg2) const
{
	calculator temp;
	temp.num = num * arg2.num;
	return temp;
}

calculator calculator::operator / (calculator arg2) const
{
	calculator temp;
	temp.num = num / arg2.num;
	return temp;
}
void main()
{
	calculator obj1, obj2, obj3;
	char ch;
	int choice;

	obj1.getdata(); // Object 1 is used for the first number
	cout << "1st number entered: ";
	cout << endl;

	obj2.getdata();
	cout << "2nd number entered: "; // Object 2 is used for the second number
	cout << endl;

	cout << "Enter either:   1: +,     2: -,     3: *, or    4:/     "; // The user chooses an option
	cin >> choice;
	cout << endl;

	switch (choice)// Gives a way to either add, subtact, multiply or divide respectively
	{
	case 1:
		obj3 = obj1 + obj2;
		break;
	case 2:
		obj3 = obj1 - obj2;
		break;
	case 3:
		obj3 = obj1 * obj2;
		break;
	case 4:
		obj3 = obj1 / obj2;
		break;
	default:
		cout << "That's an invalid choice " << endl; // Displays if you give an invalid choice
	}

	cout << "The result is "; // Displays the restult the result
	obj3.showdata();
	cout << endl;
}
I have this code I wrote myself where I overloaded operators to add, subtract, multiply and divide fractions.

Your code adds, subtracts, multiplies and divides integers, not fractions.

Note that your divide operation is dividing integers, the result of integer division is going to be truncated. i.e. 3/4=0.

I want to implement the overloading into a class.

I'm not sure I understand what you're asking. You already have a class that overloads the standard arithmetic operators. Not clear what help you're asking for.
Oh I thought that it would be able to work with fractions. Do you think that you can assist me in making it work with fractions?

And concerning the class, I asked someone if I had it implemented in the class and they told me no, that's why I asked.
Topic archived. No new replies allowed.