Question: Provide explanation of your code Create a C + + program that simulates a basic fraction calculator by overloading operators for arithmetic operations involving fractions.
Provide explanation of your code
Create a C program that simulates a basic fraction calculator by overloading operators for arithmetic operations involving fractions. This program will involve creating a Fraction class that supports these operations through operator overloading.
Part : Fraction Class Implementation
Develop a class named Fraction that encapsulates a fraction numerator and denominator and overloads operators to perform arithmetic operations.
Fraction.h Header File:
Member Variables:
o int numerator; private access
o int denominator; private access
Member Functions:
o Fraction; Default constructor
o Fractionint num, int denom; Parameterized constructor
o Fractionconst Fraction& other; Copy constructor
o int getNumerator const; Getter for the numerator
o int getDenominator const; Getter for the denominator
o void setNumeratorint num; Setter for the numerator
o void setDenominatorint denom; Setter for the denominator
o Overload the arithmetic operators and to perform operations with Fraction
objects.
o void print const; Method to display the fraction
o void simplify; Method to reduce the fraction to its simplest form
Fraction.cpp Implementation File:
o Implement the member functions, the overloaded operators, and the simplify method
declared in Fraction.h
Part : Main Function
Write the main function in a separate cpp file that uses the Fraction class to simulate a calculator.
Main Function Requirements:
o Implement a loop while or dowhile that allows for multiple operations until the user chooses to exit eg by entering E
o Randomly generate two fractions to be used in calculations the numerators and denominators are random integers between and
o Prompt the user to select an operation
o Use a switch statement to perform the selected operation using the overloaded operators.
o Display the result using the print method. Ensure the result is simplified before printing.
Implementing Automated Testing in the Main Function:
Random Number Generation :
At the start of your main function, initialize the random number generator using
srandtime
Loop for Continuous Testing:
Implement a loop while or dowhile that allows the user to perform multiple operations until they choose to exit eg by entering E
Inside the loop:
Random Number Generation :
Generate two random integers between and inclusive to be used for testing.
Creating Integer Objects:
Use the randomly generated numbers to initialize two Integer class objects.
User Operation Selection:
Prompt the user to select an operation eg Read the user's choice and store it
Implement a switch statement to handle the chosen operation.
Performing the Operation:
Based on the user's choice, perform the operation using the overloaded operators on the Integer objects.
Display the result of the operation to the user.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
