Answer to Question #242120 in C++ for William

Question #242120

Write a program to create a class ‘num’ which stores an integer number. Derive three classes from ‘num’ class namely ‘binary’, ‘octal’ and ‘hexa’ which store the binary, octal and hexadecimal equivalent of the number in ‘num’ class. Input an integer value and display its binary, octal and hexadecimal equivalent.


1
Expert's answer
2021-09-25T17:48:13-0400
#include <iostream>
#include <string>


using namespace std;




class binary;
class octal;
class hexadecimal;


class num {
public:
	num() {}
	num(int n) : the_num(n) {
		int rever[200];
		int num = the_num, i = 0;
		while (num >= 1) {
			rever[i] = (num % 2);
			i++;
			num /= 2;
		}
		int j = i-1;
		i = 0;
		
		while (j >= 0) {
			bin[i] = rever[j] + '0';
			i++; j--;
		}




		num = the_num, i = 0;
		while (num >= 1) {
			rever[i] = (num % 8);
			i++;
			num /= 8;
		}
		j = i - 1;
		i = 0;


		while (j >= 0) {
			oct[i] = rever[j] + '0';
			i++; j--;
		}


		string rever1[200];
		num = the_num, i = 0;
		while (num >= 1) {
			if ((num % 16) > 10) {
				int y = num % 16;
				switch (y) {
				case 10:
					rever1[i] = 'A';
					break;
				case 11:
					rever1[i] = 'B';
					break;
				case 12:
					rever1[i] = 'C';
					break;
				case 13:
					rever1[i] = 'D';
					break;
				case 14:
					rever1[i] = 'E';
					break;
				case 15:
					rever1[i] = 'F';
					break;
				}
			}
			else {
				rever1[i] = (num % 16) + '0';
			}
			i++;
			num /= 16;
		}
		j = i - 1;
		i = 0;


		while (j >= 0) {
			oct[i] = rever1[j];
			i++; j--;
		}
	}
	




	int the_num;
	string bin[200];
	string oct[200];
	string hexdec[200];
};




class binary : public num {
public:
	string bin;
};


class octal : public num {
public:
	string oct;
};


class hexadecimal : public num {
public:
	string hexdec;
};






int main() {
	binary B1;
	octal O1;
	hexadecimal HD1;
	num N1(61);
	cout << N1.bin;
	cout << N1.oct;
	cout << N1.hexdec;
	


	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

William
03.10.21, 10:37

Thank you AssignmentExpert

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS