SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Operator Overloading Friends And References
Exercise:
Programming Projects
Question:7 | ISBN:9780132846813 | Edition: 5

Question

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. 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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

C++ program code:

//header section
#include <iostream>
#include <cmath>
using namespace std;

//Create a class name, PrimeNumber
class PrimeNumber 
{
private:
    //Declare variable
    int prime;

public:
   //Default constructor
    PrimeNumber() 
    {
        prime = 1;
    }
    //Constructor with parameters
    PrimeNumber(int num) 
    {
        if (isPrime(num))
            prime = num;
        else
            prime = getNextPrime(num);
    }
   //The getPrime function returns the stored prime number.
    int getPrime() const
    {
        return prime;
    }
   //The prefix increment operator (++) is overloaded to 
   //find the next largest prime number and update the prime variable accordingly.
    PrimeNumber& operator++() 
    {
        prime = getNextPrime(prime + 1);
        return *this;
    }

    //The postfix increment operator (++) is overloaded to create a copy of the current object, 
    //find the next largest prime number, update the prime variable, and return the copy of the original object.
    PrimeNumber operator++(int)
    {
        PrimeNumber temp(*this);
        ++(*this);
        return temp;
    }
   //The prefix decrement operator (--) is overloaded to find 
   //the next smallest prime number and update the prime variable accordingly.
    PrimeNumber& operator--() {
        prime = getPrevPrime(prime - 1);
        return *this;
    } 
    //The postfix decrement operator (--) is overloaded to create a copy of the current object,
    //find the next smallest prime number, update the prime variable, and return the copy of the original object.
    PrimeNumber operator--(int) {
        PrimeNumber temp(*this);
        --(*this);
        return temp;
    }

private:
    //isPrime function checks whether a number is prime or not.
    bool isPrime(int num) const 
    {
        if (num <= 1)
            return false;

        for (int i = 2; i * i <= num; ++i) {
            if (num % i == 0)
                return false;
        }

        return true;
    }
    //The getNextPrime function finds the next prime number greater than the given number.
    int getNextPrime(int num) const {
        while (!isPrime(num))
            ++num;
        return num;
    }
    //The getPrevPrime function finds the next prime number smaller than the given number.
    int getPrevPrime(int num) const {
        while (!isPrime(num))
            --num;
        return num;
    }
};
//main method
int main() 
{
    //a PrimeNumber object p1 is created using the default constructor, and its prime number is output.
    PrimeNumber p1;
    cout << "Default Prime Number: " << p1.getPrime() << endl;
    //PrimeNumber object p2 is created with an initial prime number of 13, and its prime number is output.
    PrimeNumber p2(13);
    cout << "Initial Prime Number: " << p2.getPrime() << endl;
    //The prefix and postfix increment operators (++) are used on p2 to
    //find the next largest prime number, and the results are output.
    PrimeNumber p3 = ++p2;
    cout << "Next Prime Number (prefix ++): " << p3.getPrime() << endl;
    //The prefix and postfix decrement operators (--) are used 
    //on p2 to find the next smallest prime number, and the results are output.
    PrimeNumber p4 = p2++;
    cout << "Next Prime Number (postfix ++): " << p4.getPrime() << endl;

    PrimeNumber p5 = --p2;
    cout << "Previous Prime Number (prefix --): " << p5.getPrime() << endl;

    PrimeNumber p6 = p2--;
    cout << "Previous Prime Number (postfix --): " << p6.getPrime() << endl;

    return 0;
}

OUTPUT OF THE PROGRAM CODE:

Default Prime Number: 1
Initial Prime Number: 13
Next Prime Number (prefix ++): 17
Next Prime Number (postfix ++): 17
Previous Prime Number (prefix --): 17
Previous Prime Number (postfix --): 17

 

0 0

Discussions

Post the discussion to improve the above solution.