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 1: 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 Fraction(int num, int denom); (Parameterized constructor)
o Fraction(const Fraction& other); (Copy constructor)
o int getNumerator() const; (Getter for the numerator)
o int getDenominator() const; (Getter for the denominator)
o void setNumerator(int num); (Setter for the numerator)
o void setDenominator(int 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 2: 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 do-while) that allows for multiple operations until the user chooses to exit (e.g., by entering 'E').
o Randomly generate two fractions to be used in calculations (the numerators and denominators are random integers between 1 and 15).
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:
1- Random Number Generation 1:
At the start of your main function, initialize the random number generator using
srand(time(0)).
2- Loop for Continuous Testing:
Implement a loop (while or do-while) that allows the user to perform multiple operations until they choose to exit (e.g., by entering 'E').
Inside the loop:
3- Random Number Generation 2:
Generate two random integers between 1 and 10(inclusive) to be used for testing.
4- Creating Integer Objects:
Use the randomly generated numbers to initialize two Integer class objects.
5- User Operation Selection:
Prompt the user to select an operation (e.g.,'+','-','*','/'). Read the user's choice and store it.
Implement a switch statement to handle the chosen operation.
6- 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!