Here is the problem I has been trying to solve:
Define a class named PrimeNumber that stores a prime number. The default constructor 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. Finally, overload the prefix and postfix
++
and-- operators
so they return aPrimeNumber
object that is the next largest prime number (for++
) and the next smallest prime number (for--
). For example, if the object's prime number is set to 13, then invoking++
should return aPrimeNumber
object whose prime number is set to 17. Create an appropriate test program for the class.
#include <iostream>
#include <cstdlib>
using namespace std;
class PrimeNumber
{
public:
PrimeNumber() : pNum(1) {}
PrimeNumber(int setNum) : pNum(setNum) {}
int getNum() const { return pNum; }
PrimeNumber operator ++(); //prefix
PrimeNumber operator ++(int ignoreMe); //postfix
PrimeNumber operator --();
PrimeNumber operator --(int ignoreMe);
bool isPrime(const int& num);
private:
int pNum;
};
int main( )
{
int x;
cout << "enter prime number => ";
cin >> x;
PrimeNumber p(x);
PrimeNumber hold = p++;
cout << "Value of hold => " << hold.getNum() << endl;
cout << "Value of p => " << p.getNum() << endl;
cout << "enter prime number => ";
cin >> x;
PrimeNumber p2(x);
PrimeNumber hold2 = ++p2;
cout << "Value of hold2 => " << hold2.getNum() << endl;
cout << "Value of p2 => " << p2.getNum() << endl;
cout << "enter prime number => ";
cin >> x;
PrimeNumber p3(x);
PrimeNumber hold3 = p3--;
cout << "Value of hold3 => " << hold3.getNum() << endl;
cout << "Value of p3 => " << p3.getNum() << endl;
cout << "enter prime number => ";
cin >> x;
PrimeNumber p4(x);
PrimeNumber hold4 = --p4;
cout << "Value of hold4 => " << hold4.getNum() << endl;
cout << "Value of p4 => " << p4.getNum() << endl;
return 0;
}
bool PrimeNumber::isPrime(const int& num)
{
if (num < 2)
{
return false;
}
else if (num == 2)
{
return true;
}
else
{
for (int i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
return false;
}
}
}
return true;
}
PrimeNumber PrimeNumber::operator ++() //prefix
{
pNum += 1;
while (!isPrime(pNum))
{
pNum++;
}
return pNum;
}
PrimeNumber PrimeNumber::operator ++(int ignoreMe) //postfix
{
int temp = pNum += 1;
while (!isPrime(pNum))
{
temp = pNum;
pNum++;
}
return temp;
}
PrimeNumber PrimeNumber::operator --() //prefix
{
pNum -= 1;
while (!isPrime(pNum))
{
pNum--;
}
return pNum;
}
PrimeNumber PrimeNumber::operator --(int ignoreMe) //postfix
{
int temp = pNum -= 1;
while (!isPrime(pNum))
{
temp = pNum;
pNum--;
}
return temp;
}
The issue is related to decrement operator overloading. I think increment operator works as expected. If don't, please indicate. However, the hold
values don't seem as expected for postfix increment and decrement operations. How can the issue be fixed. Besides, If there is other correctable mistakes , please indicate.
Output Expected Output
---------- -------------------
enter prime number => 11 enter prime number => 11
Value of hold => 12 Value of hold => 11
Value of p => 13 Value of p => 13
enter prime number => 11 enter prime number => 11
Value of hold2 => 13 Value of hold2 => 13
Value of p2 => 13 Value of p2 => 13
enter prime number => 11 enter prime number => 11
Value of hold3 => 8 Value of hold3 => 11
Value of p3 => 7 Value of p3 => 7
enter prime number => 11 enter prime number => 11
Value of hold4 => 7 Value of hold4 => 7
Value of p4 => 7 Value of p4 => 7
hold
issue be fixed? I've edited my question @Wimmel – snr