Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with the following assignment: Define a class named PrimeNumber that

ID: 3632310 • Letter: I

Question

I need help with the following assignment:

Define a class named PrimeNumber that stores a prime number. The default con-
structor should set the prime number to 1. Add another constructor that allows the
caller to set the prime number. Also, add a function to get the prime number. Fin-ally,
overload the prefix and postfix ++ and --operators so they return a PrimeNumber
object that is the next largest prime number (for +-s-) and the next smallest prime
number (for--). For example, if the object's prime number is set to 13. then
invoking ++ should return a PrimeNumber object whose prime number is set to 17.
Create an appropriate test program for the class.

Explanation / Answer

#include <iostream>

using namespace std;

class PrimeNumber

{

public:

//default constructor

PrimeNumber();

//argumented constructor

PrimeNumber(int);

//returns prime value

int getValue();

//overloaded operators

PrimeNumber& operator ++(); //pre-increment

PrimeNumber& operator ++(int); //post-increment

PrimeNumber& operator --(); //pre-decrement

PrimeNumber& operator --(int); //post-decrement

private:

int value; //prime value

};

//Helper function

//return true if the given parameter is prime

//otherwise it returns false

bool isPrime(int n){

if( n < 1)

return false;

int i = 2;

while ( i <= n/2 ){

if ( n % i == 0)

return false;

i++;

}

return true;

}

//default constructor initializes the

//prime value to 1

PrimeNumber::PrimeNumber(){

value = 1;

}

//argumentd constructor

PrimeNumber::PrimeNumber(int p){

if(isPrime(p))

value = p;

else

{

cout<<"Invalid value for prime: "<<p<<endl;

cout<<"So the value of prime is set to 1"<<endl;

value = 1;

}

}

//return prime value

int PrimeNumber::getValue(){

return value;

}

//pre increment

PrimeNumber& PrimeNumber::operator++(){

while(!isPrime(++value));

return *this;

}

//post increment

PrimeNumber& PrimeNumber::operator++(int n){

PrimeNumber tmp = *this;

while(!isPrime(++value));

return tmp;

}

//pre decement

PrimeNumber& PrimeNumber::operator--(){

while(!isPrime(--value));

return *this;

}

//post decrement

PrimeNumber& PrimeNumber::operator--(int n){

PrimeNumber tmp = *this;

while(!isPrime(--value));

return tmp;

}

//main function to test the PrimeNumber class

int main()

{

PrimeNumber prime(13), anotherPrime;

cout << "prime: " << prime.getValue() <<endl;

cout << "anotherPrime: " << anotherPrime.getValue() <<endl;

anotherPrime = prime++;

cout<<"Post Increment anotherPrime = prime++"<<endl;

cout << "prime: " << prime.getValue() <<endl;

cout << "anotherPrime: " << anotherPrime.getValue() <<endl;

anotherPrime = prime--;

cout<<"Post Decrement anotherPrime = prime--"<<endl;

cout << "prime: " << prime.getValue() <<endl;

cout << "anotherPrime: " << anotherPrime.getValue() <<endl;

anotherPrime = ++prime;

cout<<"Pre Increment anotherPrime = ++prime"<<endl;

cout << "prime: " << prime.getValue() <<endl;

cout << "anotherPrime: " << anotherPrime.getValue() <<endl;

anotherPrime = --prime;

cout<<"Pre Decrement anotherPrime = --prime"<<endl;

cout << "prime: " << prime.getValue() <<endl;

cout << "anotherPrime: " << anotherPrime.getValue() <<endl;

return 0;

}