Answer to Question #214828 in C++ for Hemambar

Question #214828

write a program for conversion from decimal to binary ,octal and hexadecimal .the program should be developed as follows:


(a).the base class convert has two variables initial and result in which initial is a number before conversion and result is 

after conversion.


(b). it has two member functions getinit() and getconv() which return the initial value and converted value respectively.


(c). write a function compute which is a pure virtual function that actually does the conversion.


(d). separate derived classes for hexadecimal ,binary, octal, does the conversion by implementing the function compute.


(e). create the objects for each class using new operator.


(f). use the convert class type pointer to call the compute functions of the derived classes


1
Expert's answer
2021-07-11T14:38:46-0400
#include <iostream>


using namespace std;


class Convert{
    protected:
        int initial;
        int result;
    public:
        int getinit() {
            cout<<"\nEnter decimal number: ";
            cin>>initial;
            return initial;
        }
        int getconv(){
            return result;
        }
        virtual void compute()=0;
    
};
class Hexadecimal: public Convert{
    public:
        void compute(){
            int n=getinit();
            char hexaDeciNum[100];
            int i = 0;
            while (n != 0) {
                int temp = 0;
                temp = n % 16;
                if (temp < 10) {
                    hexaDeciNum[i] = temp + 48;
                    i++;
                }
                else {
                    hexaDeciNum[i] = temp + 55;
                    i++;
                }
                n = n / 16;
                }
                cout<<n<<" to Hexadecimal is ";
                for (int j = i - 1; j >= 0; j--)
                    cout << hexaDeciNum[j];
                cout<<endl;
            }
};
class Binary: public Convert{
    public:
        void compute(){
            int binaryNum[32];
            int n= getinit();
            int i = 0;
            while (n > 0) {
                binaryNum[i] = n % 2;
                n = n / 2;
                i++;
            }
            cout<<n<<" to binary is ";
            for (int j = i - 1; j >= 0; j--)
                cout << binaryNum[j];
            cout<<endl;
        }
};


class Octal: public Convert{
    public:
        void compute(){
            int n=getinit();
            int octalNum[100];
            int i = 0;
            while (n != 0) {
                octalNum[i] = n % 8;
                n = n / 8;
                i++;
            }
            cout<<n<<" to Octal is ";
            for (int j = i - 1; j >= 0; j--)
                cout << octalNum[j];
            cout<<endl;
        }
};
int main()
{
    Convert *e;
    e->getinit();
    Convert *hx= new Hexadecimal();
    hx->compute();
    Convert *b= new Binary();
    b->compute();
    Convert *oct= new Octal();
    oct->compute();
    return 0;
}

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