Friend function wont recognize privates members
For some reason in my friend function AddUp, it is not recognizing my private members. Any ideas? Thanks guys
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 173 174 175 176 177 178 179 180 181 182 183 184
|
#include <iostream>
using namespace std;
class Rational
{
private:
int numerator;
int denominator;
char signage;
public:
Rational();
friend Rational Addup(Rational first, Rational second);
void PrintFraction();
void Set_mutator1(int num1, int den1);
void Set_mutator2(int numb);
};
int main()
{
Rational first, second, total;
int num, den;
char sign;
cout << "Please enter a number for the numerator." << endl;
cin >> num;
cout << "Please enter a number for the denominator." << endl;
cin >> den;
if (den == 1) {
first.Set_mutator2(num);
}
if (den == -1) {
num = num * -1;
first.Set_mutator2(num);
}
if (den != 1 && den != -1) {
first.Set_mutator1(num, den);
}
first.PrintFraction();
cout << "Please enter a number for the second numerator." << endl;
cin >> num;
cout << "Please enter a number for the second denominator." << endl;
cin >> den;
if (den == 1) {
second.Set_mutator2(num);
}
if (den == -1) {
num = num * -1;
second.Set_mutator2(num);
}
if (den != 1 && den != -1) {
second.Set_mutator1(num, den);
}
second.PrintFraction();
total = AddUp(first, second);
return 0;
}
Rational::Rational() {
denominator = 1;
signage = '+';
}
void Rational::Set_mutator1(int num1, int den1){
int one, two;
if (num1 < 0) {
numerator = num1 * -1;
signage = '-';
one = 1;
}
else
{
numerator = num1;
signage = '+';
one = 2;
}
if (den1 < 0) {
denominator = den1 * -1;
signage = '-';
two = 1;
}
else
{
denominator = den1;
signage = '+';
two = 2;
}
if (one * two == 1) {
signage = '+';
}
if (one * two == 2) {
signage = '-';
}
else
{
signage = '+';
}
}
void Rational::Set_mutator2(int numb) {
if (numb < 0) {
signage = '-';
numerator = numb * -1;
}
else
numerator = numb;
}
void Rational::PrintFraction() {
if (denominator == 1 || denominator == -1)
cout << signage << numerator;
else
cout <<signage<<numerator<<"/"<<denominator;
}
Rational AddUp(Rational first, Rational second){
Rational total;
//numerator and denominator total for positive to negative
if (first.signage == '+' && second.signage == '-')
{
total.denominator = first.denominator*second.denominator;
total.numerator = (first.numerator*second.denominator) + (second.numerator*first.denominator*(-1));
if ((total.numerator<0 && total.denominator<0) || (total.numerator>0 && total.denominator>0))
total.signage = '+';
else
total.signage = '-';
if (total.numerator<0)
total.numerator = total.numerator*(-1);
if (total.denominator<0)
total.denominator = total.denominator*(-1);
}
//numerator and denominator total for both positive signs
if (first.signage == '+' && second.signage == '+')
{
total.denominator = first.denominator*second.denominator;
total.numerator = (first.numerator*second.denominator) + (second.numerator*first.denominator);
}
//numerator and denominator total for both negative signs
if (first.signage == '-' && second.signage == '-')
{
total.denominator = first.denominator*second.denominator;
total.numerator = (first.numerator*second.denominator) + (second.numerator*first.denominator);
total.signage = '-';
}
//numerator and denominator total for negative to positive
if (first.signage == '-' && second.signage == '+')
{
total.denominator = first.denominator*second.denominator;
total.numerator = (first.numerator*second.denominator*(-1)) + (second.numerator*first.denominator);
if ((total.numerator<0 && total.denominator<0) || (total.numerator>0 && total.denominator>0))
total.signage = '+';
else
total.signage = '-';
if (total.numerator<0)
total.numerator = total.numerator*(-1);
if (total.denominator<0)
total.denominator = total.denominator*(-1);
}
return total;
}
|
1 2
|
friend Rational Addup(Rational first, Rational second);
^
|
1 2
|
Rational AddUp(Rational first, Rational second){
^
|
Topic archived. No new replies allowed.