Answer to Question #186380 in C++ for RAHUL SINGH

Question #186380

Write a program which has a class called binary which has a character array to store a binary

string. The class decimal derives from class binary and contains an integer data member. Another

class called hexadecimal also derives from binary. Each class should contain constructors and

appropriate data members to input and display the elements. The display function of binary class

displays the binary equivalent, hexadecimal class’s display function displays hexadecimal

equivalent whereas decimal class’s display function displays the decimal equivalent.


1
Expert's answer
2021-05-01T17:21:48-0400
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
class Binary
{
public:
	Binary(const char* str);
	void Display() { cout << display << endl; }
protected:
	char* data;
	char* display;
private:
	const int MAX = 100;
};
 
Binary::Binary(const char* str)
{
	int count = 0;
	display = new char[MAX];
 
	while (str[count] != '\0')
	{
		display[count] = str[count];
		count++;
	}
 
	display[count] = '\0';
	int tempI = count;
	data = new char[count + 1];
 
	for (int i = 0; i < tempI; i++)
	{
		data[i] = display[count];
	}
	data[tempI] = '\0';
}
 
class Integer : public Binary
{
public:
	Integer(const char* str);
	Integer(int number);
	void Display() { cout << intData << endl; }
private:
	int intData;
};
 
Integer::Integer(int number) : Binary("0"), intData(number)
{}
 
Integer::Integer(const char* str) : Binary(str)
{
	int sum = 0;
	int count = 0;
	while (str[count] != '\0')
	{
		if (str[count] == '0')
		{
			count++;
			continue;
		}
		else
		{
			sum += pow(2, count);
			count++;
		}
	}
	intData = sum;
}
 
class Hexadecimal : public Binary
{
public:
	Hexadecimal(const char* str);
	void Display();
private:
};
 
Hexadecimal::Hexadecimal(const char* str) : Binary(str)
{
}
 
void Hexadecimal::Display()
{
	int i = 0;
	string temp;
	while (display[i] != '\0')
	{
		temp += display[i];
		i++;
	}
	string temp2;
	if (temp.size() % 4 == 0) temp2 = temp;
	if (temp.size() % 4 == 3)
	{
		temp2 += "0";
		temp2 += temp;
	}
	if (temp.size() % 4 == 2)
	{
		temp2 += "00";
		temp2 += temp;
	}
	if (temp.size() % 4 == 1)
	{
		temp2 += "000";
		temp2 += temp;
	}
	string temp3;
	for (int j = 0; j < temp2.size();)
	{
		temp3 += temp2[j];
		j++;
		temp3 += temp2[j];
		j++;
		temp3 += temp2[j];
		j++;
		temp3 += temp2[j];
		j++;
		if (temp3 == "0000") cout << '0';
		if (temp3 == "0001") cout << '1';
		if (temp3 == "0010") cout << '2';
		if (temp3 == "0011") cout << '3';
		if (temp3 == "0100") cout << '4';
		if (temp3 == "0101") cout << '5';
		if (temp3 == "0110") cout << '6';
		if (temp3 == "0111") cout << '7';
		if (temp3 == "1000") cout << '8';
		if (temp3 == "1001") cout << '9';
		if (temp3 == "1010") cout << 'A';
		if (temp3 == "1011") cout << 'B';
		if (temp3 == "1100") cout << 'C';
		if (temp3 == "1101") cout << 'D';
		if (temp3 == "1110") cout << 'E';
		if (temp3 == "1111") cout << 'F';
		temp3.clear();
	}
	cout << endl;
}
 
 
 
int main()
{
	char ptr[100];
	int i = 0;
	cout << "Enter binary: ";
	while (cin.peek() != '\n')
	{
		cin >> ptr[i];
		i++;
	}
	ptr[i] = '\0';
	Binary obj1(ptr);
	cout << "Binary: ";
	obj1.Display();
	cout << endl;
	cout << "Decimal: ";
	Integer obj2(ptr);
	obj2.Display();
	cout << endl;
	cout << "Hexadecimal: ";
	Hexadecimal obj3(ptr);
	obj3.Display();
	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