
Concept explainers
C++ Code:
Goal: Write a Class to represent Fractions
Write a Fraction class whose objects will represent fractions. For example, a fraction of 3/7 will be represented by a Fraction object whose numerator will be set to 3 and denominator will be set to 7.
Implementation
Note: For this checkpoint
- do not reduce fractions (i.e. 15/18 should NOT be reduced to 5/6),
- do not use "const" (since we haven't discussed in class yet)
Attributes:
- Your class should have exactly two private data members, one to represent the numerator of the Fraction being represented, and one to represent the denominator of the Fraction being represented. (What should be the data type of these member variables?)
Member functions:
- A default constructor that sets the fraction to 0 (what should be the numerator and denominator for such a fraction?).
- A parametrized constructor takes two integer arguments, a numerator and a denominator, and assigns the attributes accordingly.
- Arithmetic operations that add, subtract, multiply, and divide Fractions. They should be named addedTo, subtract, multipliedBy, and dividedBy. These return a Fraction object. In these functions you will need to declare a local "Fraction" variable, assign to it the result of the mathematical operation, and then return it.
- A boolean operation named isEqualTo that compares two Fraction objects for equality. Since you aren't reducing your Fractions, you'll need to do this by cross-multiplying. A little review: if numerator1 * denominator2 equals denominator1 * numerator2, then the Fractions are equal.
- An output operation named print that displays the value of a Fraction object on the screen in the form numerator/denominator.
Hints
- When adding or subtracting Fractions, remember that you must first find the common denominator. The easy way to do this (and that you must follow for this checkpoint since we are not simplifying fractions) is to multiply the denominators together and use that product as the common denominator.
- Here's a hint for how you will set up your arithmetic operation functions: You need two Fractions. One is the parameter, one is the current object. All operations will occur on the numerator/denominator of the current object and the numerator/denominator of the parameter. The signature of the functions implementing arithmetic operations will be something like: Fraction arithmeticOperation(Fraction& otherFraction);
- Design your class incrementally. For example, you should first implement only the constructors and the print function, and then test what you have so far. Once this code has been thoroughly debugged, you should add additional member functions, testing each one thoroughly as it is added. You might do this by creating your own client program to test the code at each stage; OR use the provided client program (see next) and comment out code that relates to member functions that you have not yet implemented.
- I am providing a template Download templatefor you that you should download. It includes the client program (main.cpp) which currently is completely commented out. To open this template code in cLion, simply use the "New Project" option, select the folder templateA, and when a prompt shows up saying "Directory is Not Empty", choose the option "Create from Existing Sources".
- The output that should be produced when the provided client program is run with your correctly implemented class is given below so that you can check your results. Subsequently, you will test your code by submitting it on zyBooks which will have a different client code.
Sample Output
The product of 9/8 and 2/3 is 18/24
The quotient of 9/8 and 2/3 is 27/16
The sum of 9/8 and 2/3 is 43/24
The difference of 9/8 and 2/3 is 11/24
The two Fractions (f1 and f2) are not equal.
The two Fractions (f3 and f4) are equal.
As you can see from the sample output given above, you are not required to reduce Fractions or change improper Fractions into mixed numbers for printing. Just print it as an improper Fraction. You are also not required to deal with negative numbers, either in the numerator or the denominator.

Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 4 images

- C:/Users/r1821655/CLionProjects/untitled/sequence.cpp:48:5: error: return type specification for constructor invalidtemplate <class Item>class sequence{public:// TYPEDEFS and MEMBER SP2020typedef Item value_type;typedef std::size_t size_type;static const size_type CAPACITY = 10;// CONSTRUCTORsequence();// MODIFICATION MEMBER FUNCTIONSvoid start();void end();void advance();void move_back();void add(const value_type& entry);void remove_current();// CONSTANT MEMBER FUNCTIONSsize_type size() const;bool is_item() const;value_type current() const;private:value_type data[CAPACITY];size_type used;size_type current_index;};} 48 | void sequence<Item>::sequence() : used(0), current_index(0) { } | ^~~~ line 47 template<class Item> line 48 void sequence<Item>::sequence() : used(0), current_index(0) { }arrow_forwardC++ coding project just need some help getting the code thank you!arrow_forwardWhat are the three tasks you must do for classes that include member variables that are pointers?arrow_forward
- Solve the questionarrow_forwardInstructions-Java Assignment is to define a class named Address. The Address class will have three private instance variables: an int named street_number a String named street_name and a String named state. Write three constructors for the Address class: an empty constructor (no input parameters) that initializes the three instance variables with default values of your choice, a constructor that takes the street values as input but defaults the state to "Arizona", and a constructor that takes all three pieces of information as input Next create a driver class named Main.java. Put public static void main here and test out your class by creating three instances of Address, one using each of the constructors. You can choose the particular address values that are used. I recommend you make them up and do not use actual addresses. Run your code to make sure it works. Next add the following public methods to the Address class and test them from main as you go: Write getters and…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





