How to change this program to work with Fractions?
Could anyone please tell me how I can turn this code I wrote into something that deals with fractions rather than integers? 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;
}
|
Topic archived. No new replies allowed.