Answered step by step
Verified Expert Solution
Question
1 Approved Answer
in C++, please solve it step by step and part by part. thank you A Mod Class 1. As you know, the mod operator can
in C++, please solve it step by step and part by part. thank you
A Mod Class
1. As you know, the mod operator can be used to find the remainder attained when you divide one number by another. For instance, 9%5 is 4, since the remainder when dividing 9 by 5 is 4. We often use mod to restrict values to a certain range. For instance, if you had 2,000 songs in your iPad playlist (numbered 0 to 1,999), and you were playing song 1,998 and wanted to skip 5 songs, the most sensible thing to do would be to play song 3, since 1998+5=2003 There is no song 2003, but 2003%2000=3
1. Write a class ModInt to represent numbers in a range from 0 to some fixed number (one less than the mod value). Your class should have variables for the mod and the numerical value, and you should provide the following functions:
o Two constructors; one that takes no arguments, and one that takes two arguments.
o Getters, but NO setters. If you want to change the value of a ModInt, instead make a new one or use =.
Define all functions besides getters outside the class definitionTest all functions in your main().
2. Add the non-member function:
bool operator ==(ModInt lhs, ModInt rhs); Returns true if a==b and false if not. lhs and rhs values and mod must both match.
Define it outside the class definition. Test the new function in your main()
3. Implement += as a member function:
void operator +=(ModInt rhs); modifies the ModInt it is called on. Youll compute the sum by adding the values of the two ModInts, then applying the mod. Note: you should only add numbers with the same mod. If someone attempts to add ModInts with different mods, update the ModInt its called on with -1 as both a mod and a value.
Why must this be a member function?
Define it outside the class definition. Test the new function in your main().
4. Explain how you would implement + as a non-member function. It should follow all the same rules as +=.
5. Implement: ModInt operator +(ModInt lhs, ModInt rhs) as a non member function. Define it outside the class definition. Test the new function in your main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started