Answer to Question #187439 in C++ for Abhinav Choudhary

Question #187439

Write a program that has an abstract class called Number having an integer data member. The class contains a pure virtual function called operation. A class called odd is derived from class called Number. Another class called Even is derived from class Number. Yet another class called prime is derived from Number. Use appropriate constructors and redefine the function called operation to display if the number is odd, even or prime. You may make use of other data members and member functions if needed.


1
Expert's answer
2021-04-30T23:14:08-0400
#include<bits/stdc++.h>
using namespace std;
class Number
{
 public:
     int nm;
     virtual void Operation(int nm)
     {
         int n;
         n=number_type(nm);
         switch(n)
         {
             case 0 : cout<<"\nNumber is even ";
                      break;
             case 1 : cout<<"\nNumber is odd ";
                      break;
             case 2 : cout<<"\nNumber is even as well as prime ";
                      break;
             case 3 : cout<<"\nNumber is odd as well as prime ";
                      break;
             default: cout<<"Invalid Number ";
         }
     }
     int number_type(int nm)
     {
         if(nm%2==0)
         {
             if(isPrime(nm))
             {
                 return 2;
             }
             else
             {
                 return 0;
             }
         }
         else
         {
             if(isPrime(nm))
             {
                 return 3;
             }
             else
             {
                 return 1;
             }
         }
     }
     bool isPrime(int n)
     {
         if (n <= 1)
            return false;


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


      return true;
     }
};
class odd : public Number
{


};
class even : public Number
{


};
class prime : public Number
{


};
int main()
{
    Number nm1;
    cout<<"Enter a number ";
    cin>>nm1.nm;
    nm1.Operation(nm1.nm);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS