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{
int n;
public:
int Decimal(){
cout<<"Enter the decimal number\n";
cin>>n;
cout<<"The decimal number is \t"<<n<<endl;
return n;
}
virtual void convert(){
}
};
class Radix2: public Radix10{
int n ;
public:
void convert(int n){
int bin[100], number=n;
int j = 0;
while (n > 0) {
bin[j] = n % 2;
n = n / 2;
j++;
}
cout<<"Binary form of "<<number<<" = ";
for (int x = j - 1; x >= 0; x--)
cout << bin[x]<<"";
}
};
class Radix16: public Radix10{
public:
void convert(int n ){
int number = n;
char arr_1[100];
int x= 0;
while(n!=0) {
int tempN = 0;
tempN = n % 16;
if(tempN < 10) {
arr_1[x] = tempN + 48;
x++;
} else {
arr_1[x] = tempN + 55;
x++;
}
n = n/16;
}
cout<<"\nThe hexadecimal representation of "<<number<<" is ";
for(int j=x-1; j>=0; j--)
cout << arr_1[j];
}
};
int main(){
Radix10 t;
int n = t.Decimal();
Radix2 r2;
r2.convert(n);
Radix16 r16;
r16.convert(n);
}
Comments
Leave a comment