I finished a program to convert from decimal to binary, octal and hexadecimal. Now I want to use that program as a jumping off point to show polymorphism in C++. I am just going to copy/paste the instructions I am working with so that I don't mess it up with my paraphrasing. Write a base class “number” that holds a single integer value (value of intInput) and contains one pure virtual member function, print_it(). Make this base class abstract in order to prevent its instantiation. Define three derived classes to print the value in hexadecimal, octal, and binary (overriding the print_it() method). Write a program to demonstrate the use of all the derived classes. The user should enter the value to be converted (intInput). So I am showing the finished code I did with the changes made to the binary conversion. Am I doing this right? Can anyone help?

//
#include<iostream>
using namespace std;

const int STACK_SIZE = 100;
class stack {
	private:
		int count;			// number of items in the stack
		int data[STACK_SIZE];
	public:
		stack();
		~stack();
		void push(const int item);	// push an item on the stack
		int pop(void);			// pop item off the stack
	};
stack::stack()	// constructor
{
	count = 0;	// zero the stack
}
stack::~stack() {}	// default destructor
void stack::push(const int item)
{
	if (count < STACK_SIZE)
	{
		data[count] = item;
		++count;
	}
	else cout << "Overflow!\n";
}
int stack::pop(void)
{
	if (count >0)
	{
		--count;
		return (data[count]);
	}
	else
	{
		cout << "Underflow!\n";
		return 0;
	}
}
int menu();
void toBinary();
void toOctal();
void toHex();

class number 
{
public:
	virtual void print_it()=0;
};

class toBin:public number
{
public:
	{
	int num;
	int total = 0;
	stack reverse; // declare a local stack!!!!!!!!!!!!!!!!!!
	int ctr=0; // declare a local counter!!!!!!!!!!!!!!!
	cout << "Please enter a decimal: ";
	cin >> num;
	cout << "The decimal number " << num << " converts to the binary number: ";
	while(num > 0)
		{
			total = num % 2;
			num /= 2;
			//cout << total << " ";
			reverse.push(total); // save to stack instead of printing!!!!!!!!!
			ctr++; // count the number of digits saved!!!!!!!!!!!!
		}
	while (ctr > 0)
             {
				cout << reverse.pop() << " ";
				ctr--;
	         }
}

int main()
{
		int choice = menu();
		switch(choice)
		{
		case (0):
			toBinary();
			break;
		case (1):
			toOctal();
			break;
		case(2):
			toHex();
			break;
		}
		return 0;
}
int menu()
{
	int choice;
	cout << " *****Menu***** " << endl;
	cout << "Convert the number from decimal into: " << endl;
	cout << "0-Binary" << endl;
	cout << "1-Octal" << endl;
	cout << "2-Hexadecimal" << endl;
	cin >> choice;
	return choice;
}

//void toBinary()
//{
//	int num;
//	int total = 0;
//	stack reverse; // declare a local stack!!!!!!!!!!!!!!!!!!
//	int ctr=0; // declare a local counter!!!!!!!!!!!!!!!
//	cout << "Please enter a decimal: ";
//	cin >> num;
//	cout << "The decimal number " << num << " converts to the binary number: ";
//	while(num > 0)
//		{
//			total = num % 2;
//			num /= 2;
//			//cout << total << " ";
//			reverse.push(total); // save to stack instead of printing!!!!!!!!!
//			ctr++; // count the number of digits saved!!!!!!!!!!!!
//		}
//	while (ctr > 0)
//             {
//				cout << reverse.pop() << " ";
//				ctr--;
//	         }
//}

void toOctal()
{
	int num;
	int total = 0;
	stack reverse; // declare a local stack!!!!!!!!!!!!!!!!!!
	int ctr=0; // declare a local counter!!!!!!!!!!!!!!!
	cout << "Please enter a decimal: ";
	cin >> num;
	cout << "The decimal number " << num << " converts to the octal number: ";
	while(num > 0)
		{
			total = num % 8;
			num /= 8;
			//cout << total << " ";
			reverse.push(total); // save to stack instead of printing!!!!!!!!!
			ctr++; // count the number of digits saved!!!!!!!!!!!!
		}
	while (ctr > 0)
             {
				cout << reverse.pop() << " ";
				ctr--;
	         }
}
void toHex()
{
	int num,counter,x,a,hex[100];
	cout<<"Please enter a decimal: ";
	cin>>num;
	cout<<"\nThe Decimal number "<<num<<" converts to the Hexadecimal number:  ";
	for(counter=0;num!=0;counter++)
		{
			a=num%16;
			hex[counter]=a;
			num=num/16;
			}
	for(x=counter-1;x>=0;x--)
		{
			if(hex[x]==10)
				{
					cout<<"A";
					}
			else if(hex[x]==11)
				{
					cout<<"B";
					}
			else if(hex[x]==12)
				{
					cout<<"C";
					}
			else if(hex[x]==13)
				{
					cout<<"D";
					}
			else if(hex[x]==14)
				{
					cout<<"E";
					}
			else if(hex[x]==15)
				{
					cout<<"F";
					}
			else
				{
					cout<<hex[x];
					}
			}
	}

Updted code: still not there but working on it...any ideas yet?

#include<iostream>
using namespace std;

const int STACK_SIZE = 100;

class stack {
	private:
		int count;			// number of items in the stack
		int data[STACK_SIZE];
	public:
		stack();
		~stack();
		void push(const int item);	// push an item on the stack
		int pop(void);			// pop item off the stack
	};
stack::stack()	// constructor
{
	count = 0;	// zero the stack
}
stack::~stack() {}	// default destructor
void stack::push(const int item)
{
	if (count < STACK_SIZE)
	{
		data[count] = item;
		++count;
	}
	else cout << "Overflow!\n";
}
int stack::pop(void)
{
	if (count >0)
	{
		--count;
		return (data[count]);
	}
	else
	{
		cout << "Underflow!\n";
		return 0;
	}
}
int menu();
//void toBinary();
//void toOctal();
//void toHex();

class number 
{
	int num;
public:
	virtual void print_it()=0;
};

class toBinary:public number
{
public:
	int num;
	int total = 0;
	stack reverse; // declare a local stack!!!!!!!!!!!!!!!!!!
	int ctr=0; // declare a local counter!!!!!!!!!!!!!!!
	cout << "Please enter a decimal: ";
	cin >> num;
	cout << "The decimal number " << num << " converts to the binary number: ";
	while(num > 0)
		{
			total = num % 2;
			num /= 2;
			//cout << total << " ";
			reverse.push(total); // save to stack instead of printing!!!!!!!!!
			ctr++; // count the number of digits saved!!!!!!!!!!!!
		}
	while (ctr > 0)
             {
				cout << reverse.pop() << " ";
				ctr--;
	         }
};

class toOctal: public number
{
public:
	void toOctal()
	int num;
	int total = 0;
	stack reverse; // declare a local stack!!!!!!!!!!!!!!!!!!
	int ctr=0; // declare a local counter!!!!!!!!!!!!!!!
	cout << "Please enter a decimal: ";
	cin >> num;
	cout << "The decimal number " << num << " converts to the octal number: ";
	while(num > 0)
		{
			total = num % 8;
			num /= 8;
			//cout << total << " ";
			reverse.push(total); // save to stack instead of printing!!!!!!!!!
			ctr++; // count the number of digits saved!!!!!!!!!!!!
		}
	while (ctr > 0)
             {
				cout << reverse.pop() << " ";
				ctr--;
	         }
};

class toHex:public number
{
		public void toHex()
    {
	int num,counter,x,a,hex[100];
	cout<<"Please enter a decimal: ";
	cin>>num;
	cout<<"\nThe Decimal number "<<num<<" converts to the Hexadecimal number:  ";
	for(counter=0;num!=0;counter++)
		{
			a=num%16;
			hex[counter]=a;
			num=num/16;
			}
	for(x=counter-1;x>=0;x--)
		{
			if(hex[x]==10)
				{
					cout<<"A";
					}
			else if(hex[x]==11)
				{
					cout<<"B";
					}
			else if(hex[x]==12)
				{
					cout<<"C";
					}
			else if(hex[x]==13)
				{
					cout<<"D";
					}
			else if(hex[x]==14)
				{
					cout<<"E";
					}
			else if(hex[x]==15)
				{
					cout<<"F";
					}
			else
				{
					cout<<hex[x];
					}
			}
	}
}

int main()
{
		int choice = menu();
		switch(choice)
		{
		case (0):
			toBinary();
			break;
		case (1):
			toOctal();
			break;
		case(2):
			toHex();
			break;
		}
		return 0;
}
number * Show;
Show = new toBinary(num);
Show->print_it();
delete Show;

Show = new toOctal(num);
Show->print_it();
delete Show;

Show = new toHex(num);
Show->print_it();
delete Show;

int menu()
{
	int choice;
	cout << " *****Menu***** " << endl;
	cout << "Convert the number from decimal into: " << endl;
	cout << "0-Binary" << endl;
	cout << "1-Octal" << endl;
	cout << "2-Hexadecimal" << endl;
	cin >> choice;
	return choice;
}

HELP? I got this to compile and run but i appear to be stuck in a loop and it doesn't cout correctly. Can anyone see where I'm going wrong?

#include<iostream>
using namespace std;

const int STACK_SIZE = 100;

class stack {
	private:
		int count;			// number of items in the stack
		int data[STACK_SIZE];
	public:
		stack();
		~stack();
		void push(const int item);	// push an item on the stack
		int pop(void);			// pop item off the stack
	};
stack::stack()	// constructor
{
	count = 0;	// zero the stack
}
stack::~stack() {}	// default destructor
void stack::push(const int item)
{
	if (count < STACK_SIZE)
	{
		data[count] = item;
		++count;
	}
	else cout << "Overflow!\n";
}
int stack::pop(void)
{
	if (count >0)
	{
		--count;
		return (data[count]);
	}
	else
	{
		cout << "Underflow!\n";
		return 0;
	}
}
int menu();


class number 
{
	public:
		number() {x=0;}
		number (int num){x=num;}
		virtual ~number() {}
		virtual void print_it()=0;
		int num;
	private:
	int x;
};

class toBinary:public number
{
public:
		void print_it()
				{
				int num;
				int total = 0;
				stack reverse; 
				int ctr=0;
				cout << "Please enter a decimal: ";  
				cin >> num;
				cout << "The decimal number " << num << " converts to the binary number: ";
					while(num > 0)
						 {
							total = num % 2;
							num /= 2;
			                cout << total << " ";
			                reverse.push(total); // save to stack instead of printing!!!!!!!!!
			                ctr++; // count the number of digits saved!!!!!!!!!!!!
		                 }
					while (ctr > 0)
					     {
							cout << reverse.pop() << " ";
							ctr--;
						 }
				}
};

class toOctal: public number
{
public:
	void print_it()
	{
		int num;
		int total = 0;
		stack reverse; 
		int ctr=0;
			cout << "Please enter a decimal: ";
			cin >> num;
			cout << "The decimal number " << num << " converts to the octal number: ";
			while(num > 0)
				{
					total = num % 8;
					num /= 8;
					cout << total << " ";
					reverse.push(total); // save to stack instead of printing!!!!!!!!!
					ctr++; // count the number of digits saved!!!!!!!!!!!!
				}
			while (ctr > 0)
			    {
					cout << reverse.pop() << " ";
					ctr--;
				}
	}
};

class toHex:public number
{
	void print_it()
	{
   		
	int num,counter,x,a,hex[100];
	cout<<"Please enter a decimal: ";
	cin>>num;
	cout<<"\nThe Decimal number "<<num<<" converts to the Hexadecimal number:  ";
	for(counter=0;num!=0;counter++)
		{
			a=num%16;
			hex[counter]=a;
			num=num/16;
		}
	for(x=counter-1;x>=0;x--)
	{
		if (hex[x] > 9)
		cout << char('A'+hex[x]-10);
	else
		cout << hex[x];
	}
	cout << endl;
	}
};

int main()

{
		int choice = menu();
		switch(choice)
		{
		case (0):
			toBinary();
			break;
		case (1):
			toOctal();
			break;
		case(2):
			toHex();
			break;
		}
		
number * Show;
Show = new toBinary();
Show->print_it();
delete Show;

Show = new toOctal();
Show->print_it();
delete Show;

Show = new toHex();
Show->print_it();
delete Show;
return 0;
}

int menu()
{
	int choice;
	cout << " *****Menu***** " << endl;
	cout << "Convert the number from decimal into: " << endl;
	cout << "0-Binary" << endl;
	cout << "1-Octal" << endl;
	cout << "2-Hexadecimal" << endl;
	cin >> choice;
	return choice;
}

Got it. Thanks for letting me work it out on here. If anyone is interested:

#include<iostream>
using namespace std;

const int STACK_SIZE = 100;

class stack {
	private:
		int count;			// number of items in the stack
		int data[STACK_SIZE];
	public:
		stack();
		~stack();
		void push(const int item);	// push an item on the stack
		int pop(void);			// pop item off the stack
	};
stack::stack()	// constructor
{
	count = 0;	// zero the stack
}
stack::~stack() {}	// default destructor
void stack::push(const int item)
{
	if (count < STACK_SIZE)
	{
		data[count] = item;
		++count;
	}
	else cout << "Overflow!\n";
}
int stack::pop(void)
{
	if (count >0)
	{
		--count;
		return (data[count]);
	}
	else
	{
		cout << "Underflow!\n";
		return 0;
	}
}
int menu();

class number 
{
	public:
		number() {x=0;}
		number (int num){x=num;}
		virtual ~number() {}
		virtual void print_it()=0;
		int num;
	private:
	int x;
};

class toBinary:public number
{
public:
		void print_it()
				{
				int num;
				int total = 0;
				stack reverse; 
				int ctr=0;
				cout << "Please enter a decimal: ";  
				cin >> num;
				cout << "The decimal number " << num << " converts to the binary number: ";
					while(num > 0)
						 {
							total = num % 2;
							num /= 2;
			                reverse.push(total); // save to stack instead of printing!!!!!!!!!
			                ctr++; // count the number of digits saved!!!!!!!!!!!!
		                 }
					while (ctr > 0)
					     {
							cout << reverse.pop() << " ";
							ctr--;
						 }
				}
};

class toOctal: public number
{
	void print_it()
	{
		int num;
		int total = 0;
		stack reverse; 
		int ctr=0;
			cout << "Please enter a decimal: ";
			cin >> num;
			cout << "The decimal number " << num << " converts to the octal number: ";
			while(num > 0)
				{
					total = num % 8;
					num /= 8;
					reverse.push(total); // save to stack instead of printing!!!!!!!!!
					ctr++; // count the number of digits saved!!!!!!!!!!!!
				}
			while (ctr > 0)
			    {
					cout << reverse.pop() << " ";
					ctr--;
				}
	}
};

class toHex:public number
{
	void print_it()
	{
   	int num,counter,x,a,hex[100];
	cout<<"Please enter a decimal: ";
	cin>>num;
	cout<<"\nThe Decimal number "<<num<<" converts to the Hexadecimal number:  ";
	for(counter=0;num!=0;counter++)
		{
			a=num%16;
			hex[counter]=a;
			num=num/16;
		}
	for(x=counter-1;x>=0;x--)
	{
		if (hex[x] > 9)
		cout << char('A'+hex[x]-10);
	else
		cout << hex[x];
	}
	cout << endl;
	}
};

int main()

{
	number * Show;
		int choice = menu();
		switch(choice)
		{
		case (0): 
			Show = new toBinary();
			Show->print_it();
			delete Show;
			break;
		case (1):
			Show = new toOctal();
			Show->print_it();
			delete Show;
			break;
		case(2):
			Show = new toHex();
			Show->print_it();
			delete Show;		
			break;
		}
return 0;
}

int menu()
{
	int choice;
	cout << " *****Menu***** " << endl;
	cout << "Convert the number from decimal into: " << endl;
	cout << "0-Binary" << endl;
	cout << "1-Octal" << endl;
	cout << "2-Hexadecimal" << endl;
	cin >> choice;
	return choice;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.