0

How do I return the flag value in operator++(int)? To make the program error-less, I had to return p. But that is not my output. If I could return the flag value, the checking of flag==0 could have occurred well. I am also doubtful whether I have written all the syntax well.is it even possible to use operator overloading in this case?i want prime operator++(prime y); to pass a value and the prime operator++(int z ) to catch the value and calculate the the flag value.

#include <iostream>
using namespace std;

class prime
{
    private:
        int i , ch ,ans ,j ,flag=0 , y , z; 
    public:
        prime()
        {
            cout <<"1. Check a no to be prime\n";
            cout <<"2. Prime no <100\n";
            cout <<"Enter your choice\n";
            cin >>ch;
            do
            {
                switch(ch)
                {
                    case 1:
                    {
                        int x=45;
                        x=y;
                        prime operator++(prime y);
                        if(flag==2)
                            cout<<"the given no. "<<y<<" is a prime no."<<endl;
                        else
                            cout<<"the given no. "<<y<<" is not a prime no."<<endl;
                        break;
                    }
                    case 2:
                    {
                        cout<<"the primes numbers below 100 are"<<endl;
                        for(i=1;i<100;i++)
                        {
                            i=y;
                            prime operator++(prime y);
                        }
                        if(flag==2)
                            cout<<i ;
                        break;
                    }
                    default:
                    {
                        cout<<"wrong choice"<<endl;
                    }
                    cout<<"press 0 to continue or 1 to exit."<<endl; 
                    cin>>ans;   
                }  // end of switch
            }while(ans==0);// end of do while
        }// end of prime constructor

        prime operator++(int z)
        { 
            prime p;
            for(j=1;j<=p.z;j++)
            {
                if(p.z%i==0)
                    flag++;
            }
            return p;
        }// end of prime operator function;
};//end of class prime

int main()
{
    prime op;
    op++;
    return 0;
}

image

  • 1
    Do not use operator overloading this way! – Eugene Sep 08 '21 at 18:47
  • 2
    If `operator++` doesn't return a reference to a `Prime`, `Prime &`, the operator will not behave as one expects the `++` operator to behave. See [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) for details. – user4581301 Sep 08 '21 at 18:47
  • 4
    `prime operator++(prime y);` doesn't call a function it declares one. – drescherjm Sep 08 '21 at 18:48
  • 2
    Side note: User input in a constructor is typically not recommended. Many things can, and often will, go wrong with user input and the only way out of a constructor in the event of an error is via thrown exception. Since users screwing up is far from exceptional behaviour, throwing an exception is often a bad choice. – user4581301 Sep 08 '21 at 18:50
  • how do i call then? please tell me ! i just learned the concept of operator overloading – Adin Shahab Sep 08 '21 at 18:50
  • 1
    @AdinShahab "*how do I call then?*" - inside of `prime` itself, you can call it like any other method, eg: `this->operator++(0);` however, it can be called in a simpler way if you dereference the `this` pointer, eg: `(*this)++;` Note, though, that you have implemented POST-increment, which must return an unmodified *copy* of `this`, but yours is not doing that. Consider implementing PRE-increment instead. – Remy Lebeau Sep 08 '21 at 18:56
  • 1
    I do not recommend specifying a name for the parameter of `operator++(int)`. The parameter is only there to make the signature different to the preincrement operator. Btw: IF you only want to provide one of the increment operations, I recommend choosing the preincrement one (`prime& operator++()`), since it doesn't require you to make a copy of the object. – fabian Sep 08 '21 at 18:59

0 Answers0