Fraction class implementation

Hi Experts,

I need help on implementing some of my Fraction class functions. I am including the whole of it, I have implemented a

small part of it but need help on the remaining part.

Fraction.h is below

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

#include <iostream>	
using namespace std;

class Fraction {

public:

	
	Fraction(int = 1, int = 1);	
		
	Fraction(const Fraction&);	

	// DESTRUCTOR
	
	~Fraction();	

	// BASIC ASSIGNEMENT OPERATOR
	
	Fraction& operator=(const Fraction&);

	// UNARY ARITHMETIC OPERATORS
	
	Fraction operator-();		// Operand 'this' implicit
	Fraction operator+();
	Fraction& operator--();		
	Fraction& operator++();		
	Fraction operator--(int);	
	Fraction operator++(int);	

	
	
	// ADVANCED ASSIGNEMENT OPERATORS
	
	Fraction& operator+=(const Fraction&);
	Fraction& operator-=(const Fraction&);
	Fraction& operator*=(const Fraction&);
	Fraction& operator/=(const Fraction&);
	Fraction& operator%=(const Fraction&);

	// ARRAY INDEX OPERATOR
	
	Fraction const& operator[](unsigned int) const;	// Read-only access
	Fraction& operator[](unsigned int);				// Read/Write access:

	// CONVERSION OPERATORS
	
	operator int();				
	operator double();			

	// SPECIAL OPERATORS
	
	Fraction operator!();		// Inverts a fraction
	int operator*();			// Gets the Numerator
	unsigned int operator~();	// Gets the Denominator

	
	friend ostream& operator<<(ostream&, const Fraction&);
	friend istream& operator>>(istream&, Fraction&);

	
	// CONSTANTS OF DATATYPE
	
	static const Fraction	sc_fUnity;	// Defines 1/1
	static const Fraction	sc_fZero;	// Defines 0/1

	// STATIC UTILITY FUNCTIONS
	
	static const int precision() 	{ return 1000000; };

	static const Fraction	sc_fUnity;	// Defines 1/1
private:

	// DATA MEMBERS
	int				iNumerator_;	// The Numerator
	unsigned int	uiDenominator_;	// The Denominator 

	// STATIC UTILITY FUNCTIONS
	
	  static int gcd(int, int);	// Finds the gcd for two +ve integers
	  static int lcm(int, int);	// Finds the lcm for two +ve integers
};




Main.cpp is below:
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
#include <iostream>
using namespace std;

#include "Fraction.h"
#include<cmath>

// Test code for Fraction class
int main()
{
	Fraction f1(5, 3);
	Fraction f2(7);
	Fraction f3;
	Fraction f5(55, 33);

	// BASIC ASSIGNEMENT OPERATOR
	
	// Fraction& operator=(const Fraction&);
	cout << "Assingment (Before): f3 = " << f3 << ". f1 = " << f1 << endl;
	f3 = f1;
	cout << "Assingment (After): f3 = " << f3 << ". f1 = " << f1 << endl;
	f1 = Fraction(5, 12);


	// UNARY ARITHMETIC OPERATORS
	
	// Fraction operator-();		// Operand 'this' implicit
	f3 = -f1;
	cout << "Unary Minus: f3 = " << f3 << ". f1 = " << f1 << endl;

	// Fraction operator+();

	// Fraction operator--();		// Pre-decrement. Dividendo
	f5 = Fraction::sc_fUnity;
	cout << "Pre-Decrement (Before): f3 = " << f3 << ". f5 = " << f5 << endl;
	f3 = --f1;
	cout << "Pre-Decrement (After): f3 = " << f3 << ". f1 = " << f1 << endl;

	// Fraction operator--(int);	// Post-decrement. Lazy Dividendo
	f3 = Fraction::sc_fUnity;
	cout << "Post-Decrement (Before): f3 = " << f3 << ". f1 = " << f1 << endl;
	f3 = f1--;
	cout << "Post-Decrement (After): f3 = " << f3 << ". f1 = " << f1 << endl;

	// Fraction operator++();		// Pre-increment. Componendo
	f3 = Fraction::sc_fUnity;
	cout << "Pre-Increment (Before): f3 = " << f3 << ". f1 = " << f1 << endl;
	f3 = ++f1;
	cout << "Pre-Increment (After): f3 = " << f3 << ". f1 = " << f1 << endl;

	// Fraction operator++(int);	// Post-increment. Lazy Componendo
	f3 = Fraction::sc_fUnity;
	cout << "Post-Increment (Before): f3 = " << f3 << ". f1 = " << f1 << endl;
	f3 = f1++;
	cout << "Post-Increment (After): f3 = " << f3 << ". f1 = " << f1 << endl;



	

	// ADVANCED ASSIGNEMENT OPERATORS
	
	// Fraction& operator+=(const Fraction&);
	f1 = Fraction(5, 12);
	f2 = Fraction(7, 18);
	f3 = f2;
	f2 += f1;
	cout << "+=: f2 = " << f2 << ". f1 = " << f1 << ". f2 (before) = " << f3 << endl;
	f3 = f2;
	f2 += f2;
	cout << "+=: f2 = " << f2 << ". f2 (before) = " << f3 << endl;

	// Fraction& operator-=(const Fraction&);
	f1 = Fraction(5, 12);
	f2 = Fraction(7, 18);
	f3 = f2;
	f2 -= f1;
	cout << "-=: f2 = " << f2 << ". f1 = " << f1 << ". f2 (before) = " << f3 << endl;
	f3 = f2;
	f2 -= f2;
	cout << "-=: f2 = " << f2 << ". f2 (before) = " << f3 << endl;

	// Fraction& operator*=(const Fraction&);
	f1 = Fraction(5, 12);
	f2 = Fraction(7, 18);
	f3 = f2;
	f2 *= f1;
	cout << "*=: f2 = " << f2 << ". f1 = " << f1 << ". f2 (before) = " << f3 << endl;
	f3 = f2;
	f2 *= f2;
	cout << "*=: f2 = " << f2 << ". f2 (before) = " << f3 << endl;

	// Fraction& operator/=(const Fraction&);
	f1 = Fraction(5, 12);
	f2 = Fraction(7, 18);
	f3 = f2;
	f2 /= f1;
	cout << "/=: f2 = " << f2 << ". f1 = " << f1 << ". f2 (before) = " << f3 << endl;
	f3 = f2;
	f2 /= f2;
	cout << "/=: f2 = " << f2 << ". f2 (before) = " << f3 << endl;

	// Fraction& operator%=(const Fraction&);
	f1 = Fraction(7, 18);
	f2 = Fraction(5, 12);
	f3 = f2;
	f2 %= f1;
	cout << "%=: f2 = " << f2 << ". f1 = " << f1 << ". f2 (before) = " << f3 << endl;
	f3 = f2;
	f2 %= f2;
	cout << "%=: f2 = " << f2 << ". f2 (before) = " << f3 << endl;

	// ARRAY INDEX OPERATOR
	
	// Fraction& operator[](unsigned int);
	Fraction f[2];
	f1 = Fraction(7, 18);
	f2 = Fraction(5, 12);
	f[0] = f1;
	f[1] = f2;
	cout << "[]: f[0] = " << f[0] << ". f[1] = " << f[1] << endl;

	// CONVERSION OPERATORS
	
	// operator int();
	f[0] *= 11;
	cout << "(int): (int)f[0] = " << (int)f[0] << ". f[0] = " << f[0] << endl;

	// operator double();
	cout << "(double): (double)f[0] = " << (double)f[0] << ". f[0] = " << f[0] << endl;

	// SPECIAL OPERATORS
	
	// Fraction operator!();	// Inverts a fraction
	f[1] *= 11;
	cout << "Inversion: 1/f[1] = " << !f[1] << ". f[1] = " << f[1] << endl;

	// int operator&();				// Gets the Numerator
	f1 = Fraction(7, 18);
	cout << "Numerator: f1.Num = " << &f1 << ". f1 = " << f1 << endl;

	// unsigned int operator~();	// Gets the Denominator
	cout << "Denominator: f1.Deno = " << ~f1 << ". f1 = " << f1 << endl;

	// COMPOSITE SCENARIO
	f1 = Fraction(7, 18);
	f2 = Fraction(5, 12);
	f3 = Fraction(16, 9);
	f[0] = f1;
	f[1] = f2;

	Fraction fTmp;

	fTmp = f1 - f2++ * f3 + f[0]/f[1];
	cout << "Composite: fTmp = " << fTmp << ". f1 = " << f1 << ". f2 = " << f2 << ". f3 = " << f3 << ". f[0] = " 

<< f[0] << ". f[1] = " << f[1] << endl;


	return 0;
}


Fraction.cpp, that I have done a bit but need help is below

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
#include "Fraction.h"
#include <cmath>

Fraction::Fraction(int n, int d):iNumerator_(n),uiDenominator_(d)
{
	cout << "Normal Constructor (2 Params): " <<*this<<"\n";
}



const Fraction Fraction::sc_fUnity(1,1);




Fraction::Fraction(const Fraction& f):iNumerator_(f.iNumerator_),uiDenominator_(f.uiDenominator_)
{
	cout <<"Copy constructor"<<endl;
}

// The destructor body

Fraction::~Fraction()
{
	cout <<"Destructor :"<<*this<<" Objects: "<<"\n";
}


Fraction& Fraction::operator= (const Fraction &rightSide)
{
	iNumerator_ = rightSide.iNumerator_;
	uiDenominator_ = rightSide.uiDenominator_;
	return *this;
}


ostream& operator<<(ostream& os, const Fraction& f1)
{
	//cout << "Indirection ostream func" << endl ; 
	os << '(' << f1.iNumerator_ << ", " << f1.uiDenominator_ << ')';

	return os;
}


Topic archived. No new replies allowed.