Question: C++ overloading operators. Thank you! Code to use: #include #include #include using namespace std; class my_int { public: //********************************************************* // Function: my_int // Purpose: Constructor
C++ overloading operators. Thank you!


Code to use:
#include #include #include using namespace std;
class my_int { public: //********************************************************* // Function: my_int // Purpose: Constructor initializes the val to x // Params: x - the value for the val // Calls: none // Uses: none //********************************************************* my_int(int x);
//********************************************************* // Function: my_int // Purpose: Constructor initializes the val to 0 // Params: none // Calls: none // Uses: none //********************************************************* my_int();
//********************************************************* // Function: set // Purpose: Sets the val to x // Params: x - the new value for the val // Calls: none // Uses: none //********************************************************* void set(int x);
//********************************************************************* // Function: input // Purpose: reads and stores a value from inp. if fin is a input // stream, then fin is already connected to a file.User enters // a value and ask the user to re-enter the data if the // user enters an incorrect value. // Params: inp -- the input stream // Calls: none // Uses: istream //********************************************************************* void input(istream& inp); //********************************************************************* // Function: output // Purpose: display the val on fout. if fout is a output stream // then fout is already connected to a file // Params: fout -- the output stream // Calls: none // Uses: ostream //********************************************************************* void output(ostream& fout) const;
//********************************************************* // Function: get_int // Purpose: returns the val // Params: none // Calls: none // Uses: none //********************************************************* int get_int();
private: int val; // Variable to hold value for class };
//********************************************************* // Function: is_prime // Purpose: object num contains a valid positive value // returns true if num is prime; otherwise // returns false // Params: num - the value to be checked for prime // Calls: sqrt // Uses: cmath //********************************************************* bool is_prime(const my_int& num);
int main() { my_int value1; value1.input(cin); value1.output(cout);
if (is_prime(value1)) cout
//********************************************************* // Function: my_int // Purpose: Constructor initializes the val to x // Params: x - the value for the val // Calls: none // Uses: none //********************************************************* my_int::my_int(int x) { val = x; }
//********************************************************* // Function: my_int // Purpose: Constructor initializes the val to 0 // Params: none // Calls: none // Uses: none //********************************************************* my_int::my_int() { val = 0; }
//********************************************************* // Function: set // Purpose: Sets the val to x // Params: x - the new value for the val // Calls: none // Uses: none //********************************************************* void my_int::set(int x) { val = x; }
//********************************************************************* // Function: output // Purpose: display the val on fout. if fout is a output stream // then fout is already connected to a file // Params: fout -- the output stream // Calls: none // Uses: ostream //********************************************************************* void my_int::output(ostream& fout) const { fout
limit = sqrt(static_cast(num.get_int())); n = 2;
while (n
void my_int::input(istream& inp) {
inp>>val; }
int my_int::get_int() { input(cin); return val; }
3. overloading operators Overload the operator as a friend function to the vint class. Include the function declaration and the function definition. Also include the necessary statements in the main to test the function you have written. You will have to create two objects of wwiRt type with values you choose. Then compare and see whether you're overloaded operator is working correctly. Don't make any changes to the existing statements in the main function). Don't forget to include the documentation for this function. Look at question ld on the answer sheet for the point distribution. For more information, look at pages 644-646 in your text
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
