increment and decrement operator overloading

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 a PrimeNumber 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 a PrimeNumber 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

PrimeNumber PrimeNumber::operator --(int ignoreMe) //postfix
{
    int temp = pNum -= 1;
    while (!isPrime(pNum))
    {
        temp = pNum;
        temp--;
    }
    return temp;
}

this code will run forever, cause pNum is not changed inside loop. Your program just hangs after last input, trying to execute postfix operator-- .

Edit

Your postfix operator:

PrimeNumber PrimeNumber::operator --(int ignoreMe) //postfix
{
    int temp = pNum -= 1;
    while (!isPrime(pNum))
    {
        temp = pNum;
        pNum--;
    }
    return temp;
}

is still invalid. This is correct version IMHO:

PrimeNumber PrimeNumber::operator --(int ignoreMe) //postfix
{
    int temp = pNum;
    while (!isPrime(--pNum)) {}
    return temp;
}

the same for oprator++(int) :

PrimeNumber PrimeNumber::operator ++(int ignoreMe) //postfix
{
    int temp = pNum;
    while (!isPrime(++pNum)) {}
    return temp;
}

So there's some basic issues with your code.

  • In general, we want prefix operators to return a reference to themselves.
  • Your return values in general are calling an unnecessary implicit PrimeNumber constructor.
  • Here's a basic rule you can follow:

  • Prefix style: Perform the increment or decrement as appropriate, and then return *this .

  • Postfix style : Create a copy of yourself by doing auto copy = *this , and then call the appropriate prefix operator.

  • Let's try implementing this:

    PrimeNumber & PrimeNumber::operator ++() //prefix
    {
        pNum += 1;
        while (!isPrime(pNum))
            pNum++;
        return *this;
    }
    
    PrimeNumber PrimeNumber::operator ++(int ignoreMe) //postfix
    {
        auto copy = *this;
        ++(*this);
        return copy;
    }
    
    PrimeNumber & PrimeNumber::operator --() //prefix
    {
        pNum -= 1;
        while (!isPrime(pNum))
            pNum--;
        return *this;
    }
    
    PrimeNumber PrimeNumber::operator --(int ignoreMe) //postfix
    {
        auto copy = *this;
        --(*this);
        return copy;
    }
    

    Additionally, test code should throw assertions or the like when they give the wrong result. I shouldn't need to think about if a test is right or wrong.

    int main( ) {
        auto x = PrimeNumber(17);
        auto hold = x++;
        assert(hold.getNum() == 17);
        assert(x.getNum() == 19);
    
        x = PrimeNumber(18);
        hold = x--;
        assert(hold.getNum() == 18);
        assert(x.getNum() == 17);
    
        x = PrimeNumber(25);
        hold = ++x;
        assert(hold.getNum() == 29);
        assert(x.getNum() == 29);
    
        x = PrimeNumber(19);
        hold = --x;
        assert(hold.getNum() == 17);
        assert(x.getNum() == 17);
    
        cout << "Success!n"
    }
    

    This main() will only print Success! if all of the assertions are true.

    链接地址: http://www.djcxy.com/p/73150.html

    上一篇: C ++模板类和运算符重载

    下一篇: 递增和递减运算符重载