Hi. So my homework assignment for my C++ class this week is to create a class that converts from decimal to binary (well, actually it is to binary, octal, and hex, but I am starting with just binary until I figure it out). I am fine on the conversion part, but actually getting the class to work with the code I have in main() is giving me trouble. Before I even get to the more thorough explanation, I am going to post my code, since I hardly even know where to start:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <iostream>
using namespace std;
#include "stack.h"
class number
{
public:
virtual void print_it() = 0;
protected:
int intInput;
float remainder, intBuffer;
stack s;
};
class binary : public number
{
public:
binary();
~binary();
void print_it()
{
while (intInput > 0)
{
remainder = intInput % 2;
intInput /= 2;
s.push(remainder);
}
while(s.getCount() > 0)
{
intBuffer = s.pop();
cout << intBuffer;
}
}
};
int main ()
{
int intInput;
cout << "Please enter a decimal: ";
cin >> intInput;
number* Example;
Example = new binary(intInput); // No matching constructor for initialization of 'binary'
Example->print_it();
delete Example;
return 0;
}
|
My header file contains the class stack. If you want to see the contents of the header, the code is down below. The file was given to me by my professor, so there is nothing wrong with it (I also used it in my assignment from last week, and it came out fine).
Anyways, as you can probably tell by my code above, I have no idea what I am doing here. My assignment first asks to create a base class "number" that has an integer as member data ("value of intInput," as the assignment says) and has a pure virtual function print_it(). I am then supposed to derive a class binary, which overrides the print_it() method and converts the user's decimal input to binary. The assignment gives me this code to use in main:
1 2 3 4
|
number* Example;
Example = new binary(intInput);
Example->print_it();
delete Example;
|
I am confused here, because I am supposed to be using a parameter for this declaration of binary. The only problem is, I have no idea why there is a parameter or how I would fix the binary class to use it.
So I suppose my biggest problem here is not knowing what the heck to do about that parameter. Also, I am pretty sure I do not need to declare intInput in both the base class and in main(), and I probably declared remainder, intBuffer, and s in unideal places. Or maybe not. Anyways, I would really appreciate it if someone could help me work out what I need to do to get my class working, and maybe clean up the program a little.
In case you need to know, my header file contains the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#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(); // pop item off the stack
int getCount() const; // return count
};
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()
{
if (count >0)
{
--count;
return (data[count]);
}
else
{
cout << "Underflow!\n";
return 0;
}
}
int stack::getCount() const
{
return count;
}
|