C++ overloading operators. Thank you! Code to use: #include #include #include us
ID: 3821804 • Letter: C
Question
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<<" is a prime number ";
else
cout<<" is not a prime number ";
return 0;
}
//*********************************************************
// 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<<" The value is equal to "<< val;
while (val <= 1)
{
cout<<"Entered an Invalid value ";
cout<<"Enter a positive value greater than 1 ";
input(cin);
}
}
//*********************************************************
// 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)
{
double limit;
int n; //divisor
bool prime= true;
limit = sqrt(static_cast(num.get_int()));
n = 2;
while (n <= limit && prime)
{
if (num.get_int() % n == 0)
prime = false;
else
n++;
}
return prime;
}
void my_int::input(istream& inp) {
inp>>val;
}
int my_int::get_int() {
input(cin);
return val;
}
Explanation / Answer
Here is the update for you:
#include <iostream>
#include <cmath>
#include <fstream>
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();
//*********************************************************
// Function: operator>
// Purpose: Overloads the > operator.
// Params: myInt1, myInt2 --- the objects to be compared.
// Calls: get_int()
// Uses: none
//*********************************************************
friend bool operator>(const my_int&, const my_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<<" is a prime number ";
else
cout<<" is not a prime number ";
my_int value2;
value2.input(cin);
if(value1 > value2)
{
value1.output(cout);
cout << " is greater than ";
value2.output(cout);
cout << endl;
}
else
{
value1.output(cout);
cout << " is less than or equal to";
value2.output(cout);
cout << endl;
}
return 0;
}
//*********************************************************
// 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<<" The value is equal to "<< val;
while (val <= 1)
{
cout<<"Entered an Invalid value ";
cout<<"Enter a positive value greater than 1 ";
input(cin);
}
}
//*********************************************************
// 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)
{
double limit;
int n; //divisor
bool prime= true;
limit = sqrt(static_cast<double>(num.get_int()));
n = 2;
while (n <= limit && prime)
{
if (num.get_int() % n == 0)
prime = false;
else
n++;
}
return prime;
}
void my_int::input(istream& inp) {
inp>>val;
}
int my_int::get_int() {
input(cin);
return val;
}
//*********************************************************
// Function: operator>
// Purpose: Overloads the > operator.
// Params: myInt1, myInt2 --- the objects to be compared.
// Calls: none
// Uses: none
//*********************************************************
bool operator>(const my_int& myInt1, const my_int& myInt2)
{
return myInt1.val > myInt2.val;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.