Create a base class called Radix10 which has a member function decimal. It can be set and display
by member functions. It also contains a virtual function called convert. Derive two classed from
Radix10. One is called Radix2 and the next is called Radix16. They redefine the convert function
such that the decimal value is displayed into binary or hexa_decimal. They have a data member
length which can be set by member functions. To convert the decimal data is divided by appropriate
radix length times and the remainder which is always from 0 to (radix-1) should be displayed in
reverse order. In hexa decimal, for remainder 10, 11, 12 ,13, 14 and 15 the codes A, B, C, D, E or F
should be used respectively.
#include <iostream>
using namespace std;
class Radix10{
public:
void decimal(){
}
virtual void convert(){
}
};
class Radix2:public Radix10{
int length;
public:
int decimal(){
cout<<"\nEnter a decimal number to convert to binary: "<<endl;
cin>>length;
return length;
}
void convert(){
int binaryNum[32];
int n= decimal();
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 Radix16:public Radix10{
int length;
public:
int decimal(){
cout<<"\nEnter a decimal number to convert to Hexadecimal: "<<endl;
cin>>length;
return length;
}
void convert(){
int n=decimal();
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;
}
};
int main()
{
Radix2 r1;
r1.decimal();
r1.convert();
Radix16 r2;
r2.decimal();
r2.convert();
return 0;
}
Comments
Leave a comment