I need help with my project for my c++ class. I am a little confused on how to get started. I am making a class called "bigint" and i have to have a global constant "size". How do I make that global "size" the size of bigint? If "size" is an array and "bigint" is an object how does that work? Heres the instructions for the program..
Requirements:
-You must use the class construct to implement your ADT.
-The ADT bigint need only work for positive numbers.
-Use a global constant value for the maximum size of the bigint that is, a constant sized array.
Implementation:
-The size of the bigint must be specified by a global constant
-A method (constructor) to initialize a bigint to zero.
-A method (constructor) to initialize a bigint to an int value you provide [0, maxint]. Example: bigint(128).
-A method (constructor) to initialize a bigint to a char[] you provide. You can assume what is provided is a valid bigint. Example: bigint("299793").
-A method to write a bigint that prints at most 60 digits per line.
-A method to compare if two bigints are equal. It should return a bool - true if equal and false otherwise.
The global constant just means that it needs to be defined in the .h file for this, and outside of a function. The person who answered before me has it laid out for you. The size is almost an arbitrary number in this case. In lab we picked 500, so that is just what I am sticking with for the time being.
Everything but the output looks good. Remember that with the print you are not passing the bigint, you are passing ostream by ref and giving it a new short name.
Might as well write the classic std::ostream& operator<<(std::ostream&, const YourClass&) overload if you're going to pass a reference to std::ostream for output.
We have the user input a value (int) for bigint. But which element of the array do we put it in? I was confused at the point of making bigint an array if we really only need it to hold one value.
Sounds like what I would do(have done). Though I don't want to mislead you if your assignment actually calls for something else. You should ask your instructor for clarification (or madmuse since he/she seems to be in your class).
You have to do both. Initializing it with both a char and int allows you to enter all the numbers that exist. You have the line of code for initializing it as a char up there already. You just need to write a method that converts it from chars to int values.
#ifndef BIGINT.H
#define BIGINT.H
constint MAZ_SIZE = 500; //Can be anything that would be a 500 digit..
class BigInt
{
public:
BigInt( void ); //default that sets it equal to 0
BigInt( constint value ); //I like to name in constructor so you know whats in it
BigInt( constchar *value ); //takes the character array in
friend std::ostream operator<<( std::ostream &stm , const BigInt &bigi ); //output
friendbooloperator==( const BigInt &rhs , const BigInt &lhs ); //compare
private:
int value[ MAX_SIZE ];
};
*on a side note..
You can do the .output() and compare() functions if you want
But they should look like
1 2 3
public:
void output( void ); // output();
bool compare( const BigInt &rhs ); //*this will be your left hand side.
So for your compare it could be something like
1 2 3 4 5
bool BigInt::compare( const BigInt &rhs )
{
return( value == rhs.value ); //this will be different based on how you store your value. If you do the array method which you are supposed to
//you should use a loop
}
I cant seem to figure out the char function. How do we make it an array of characters if its an int array? I got my int constructor to work but I cant figure out this char one.. Can someone get me started?
So if I had a user input '128' for example. How could I access the first char to be able to change it to an integer? I need to somehow convert '128' in char to 128 in int basically. For integers it's easier because I can do things like
1 2 3 4 5 6 7
int numdigits = log10((double)digit) + 1;
for (int i=numdigits-1; i >= 0; i--)
{
value[i] = digit%10;
digit /= 10;
}