Overloading

Can some one help with this assignment, new to C++ AND NEED to improve my code below to overload the addition operator, the is equal to operator, the equal to operator for an abstract data type, and perform a class to built-in conversion.Program requirements at the bottom page

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
 #include <iostream>
using namespace std;
// class declaration section
class Fraction
{
private:
	int Numerator;
	int Denominator;
public:
	Fraction(); // default constructor
	Fraction(int, int); //overloaded constructor
	void setNumerator(int); 
	int getNumerator();
	void setDenominator(int);
	int getDenominator();
};
// class implementation section//
Fraction::Fraction()//default constructor
{
	//assign the Numerator and Denominator to 1
	Numerator = 1;
	Denominator = 1;
}
Fraction::Fraction(int num, int den)//overloaded constructor
{
	//assign Numerator to value passed for the numerator
	Numerator = num;
	//assign the Denominator to 1 if the value passed to the function is 0
	if (den == 0)
		Denominator = 1;//set denominator to 1 
	//otherwise assign the value passed to the Denominator
	else
		Denominator = den;
}
	
void Fraction::setNumerator(int numeratorArg){		
		Numerator = numeratorArg;
}
int Fraction::getNumerator(){
	return Numerator;
}
void Fraction::setDenominator(int denominatorArg){
	if (denominatorArg > 1){
		Denominator = denominatorArg;
	}
	else if (denominatorArg == 0){
		Denominator = 1; //assign to one if value passed to functino is 0
	}
}
int Fraction::getDenominator(){
	return Denominator;
}
int main()
{
	Fraction F1, F2(3, 4); //instantiate 2 Fraction objects
	cout << "Value of the first Fraction object is " << F1.getNumerator() << "/" << F1.getDenominator() << endl;
	cout << "Value of the second Fraction object is " << F2.getNumerator() << "/" << F2.getDenominator() << endl;
	//change the values of one of the Fraction objects
	F2.setNumerator(8);
	F2.setDenominator(5);
	cout << "\nAfter changing the values of the second fraction object" << endl;
	cout << "Value of the second Fraction object is " << F2.getNumerator() << "/" << F2.getDenominator() << endl;
	return 0;
}


Program Requirements:
Modify the Fraction class created in the previous assignment.
Include an overloaded equality operator (==) in the Fraction class that tests two Fraction objects for equality. This function should return back true if the two Fraction objects are equal and false if the two Fraction objects are not equal. The two Fraction objects are equal if the value of the numerator data members are equal and the value of the denominator data members are equal.
Include an overloaded assignment operator (=) in the Fraction class that assigns one Fraction object to another. The return type of this operator function should be a Fraction object to allow multiple assignments such as frac1 = frac2 = frac3; You must write the code for this operator, you cannot use the default assignment operator the C++ compiler will build in the absence of one (page 499).
Include an overloaded addition operator, operator+, that will add two Fraction objects together. This function will return back a new Fraction object that is the sum of two existing Fraction objects.
Include a class to built-in conversion that will return the value of a Fraction object as a double. Divide the numerator by the denominator to get this result.
Make sure all your methods are defined in the implementation section of the class. Do not use any inline member functions. Do not print from within the Fraction class.
Steps 8 through 14 below all apply to the main function.
Prompt the user for a numerator and denominator. Enter these from the keyboard. Use the appropriate constructor and/or functions to create a Fraction object using the values input.
Prompt the user for a second numerator and denominator. Enter these from the keyboard. Use the appropriate constructor and/or functions to create a second Fraction object using the values input.
Use the overloaded == operator to display a message indicating whether the two Fraction objects from steps 8 and 9 are the same or different.
Use the overloaded addition operator to add the two Fraction objects together and display the values of the resulting Fraction object.
Use the overloaded assignment operator to assign the first Fraction object created in step 8 to the second Fraction object created in step 9.
Use the overloaded == operator again to display a message indicating whether the two Fraction objects are the same or different.
Call the class to built-in conversion function created in step 5 to convert one of the Fraction objects to a double and display this value.
Take a look at this:

http://www.cprogramming.com/tutorial/operator_overloading.html

It shows how to do that.
Topic archived. No new replies allowed.