I have an assignment to create a program that reads 2 fractions and print their product. I need to have 3 constructors, one defaults, one that allows us to create a fraction that represents a whole number, and the third constructor should allow us to specify the numerator and denominator of a new fraction object.
I have to test the function with the fractions 3/4 and 5/6 and also with 1/2 and 1.
I can't make the readFraction function take the number one, without the slash and a denominator.
My prof. suggested that I should use: Fraction(constint &num){ myNumerator = num;myDenominator = 1; } //defines the fraction as a whole number
But I couldn't make it work. So I think that the readFraction function is the problem.
Here is my code.
First, you should not be using #include "fraction.cpp"
You should not have to include .cpp files.
Second, why don't you use inheritance and make a parent class Fraction and subclasses NoNumeratorFrac and WholeNumFrac.
That would also help you with OOP.
I put #include "fraction.cpp"
because I need to fix the settings of my compiler and I don't know how. I will get to it when I have a chance.
Can you please explain a little more in detail what you mean with the two subclasses. Why isn't there a normal fraction subclass. And how the compiler will know which class to use, when the user enters the fraction? The user may enter a regular fraction or a whole number. The different subclasses will allow me to have different readFraction() fuctions, but how can I tell the compiler to use one class if the user enters a regular fraction, or another class if the y enter a whole number?
Well, you can tell which type of fraction you are using at compile time. You are initializing three fractions yourself, so you know which class to use. I guess that you are trying to have users enter their own fractions. That would be harder. So maybe you shouldn't have subclasses for such a small program. Maybe you are not entering the slash when you enter the fraction and the program does not "see" the slash? Just make sure you type the slash at the command line.
Does that help?
I tried, but the program will not show the results, if I don't type a slash and another number, because this is how my readFunction is. I was wondering, can I add something to the construcor or the readFunction that could help?