(Array class template) Use class Array as a class template. Demonstrate the new
ID: 642874 • Letter: #
Question
(Array class template) Use class Array as a class template. Demonstrate the new Array class template in a program.
Create a class RationalNumber(fractions) with the following capabilities:
a. Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions that are not in reduced form and avoids negative denominators.
b. Overload the addition, subtraction, multiplication and division operators for this class
c. Overload the relational and equality operators for this class
Explanation / Answer
#include <iostream.h>
#include <stdlib.h>
//if you want to use cout and endl later, you should add this line:
using namespace std;
class fraction
{
private:
int den, num;
bool checkdata();
public:
fraction(); //a default constructor is not very useful in this case.
fraction(int aNum,int aDen); //this can be used to initialize den and num.
void print();
//the operators need to operate on objects of class "fraction", as follows:
fraction operator+(fraction); //actual should be "const fraction&", but for now, this is OK
fraction operator-(fraction);
fraction operator/(fraction);
fraction operator*(fraction);
};
/*void fraction:: checkdata()
{
if (den=0 && den<0) //here (den=0) will assign the value 0 to den, it should be den==0 to test if den is equal to 0.
}*/ //this function has no effect that way
//this is more useful:
bool fraction::checkdata() //you can return a bool that signifies whether the test passed or not.
{
return den != 0; //test that denominator is not zero.
}
fraction fraction::operator+(fraction a)
{
//fill this yourself
return fraction; //just return 0 for now.
}
fraction fraction::operator-(fraction a)
{
//fill this yourself
return fraction; //just return 0 for now.
}
fraction fraction::operator/(fraction a)
{
//fill this yourself
return fraction; //just return 0 for now.
}
fraction fraction::operator*(fraction a)
{
//this is the simpler one, so I will show how to implement it:
int newDen = den * a.den;
int newNum = num * a.num; //just "num" takes the value of num stored in this object and a.num takes the value of num stored in object "a".
return fraction(newDen,newNom); //see constructor below.
}
fraction::fraction()
{
//checkdata(); //this is a useless constructor, you need to give initial values:
den = 1;
num = 0; //initialize to zero with a proper denominator.
}
//this is a more useful constructor:
fraction::fraction(int aNum,int aDen)
{
den = aDen;
num = aNum;
};
void fraction::print()
{
cout << num << "/" << den << endl;
}
main()
{
fraction one(1,2); //initialize to 1/2
fraction two(1,6); //initialize to 1/6
fraction obj; //the results of the operations are fractions too.
obj=one+two; obj.print(); //just print the results.
obj=one-two; obj.print();
obj=one*two; obj.print();
obj=one/two; obj.print();
//print();//this print function is part of fraction, it is not global, so it needs to be called on an object (like the above obj.print() examples).
system('pause');
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.