I'm very new to classes and while I have all other code written, I am stuck lacking some implementation at the end of two of my member functions.
Here is my header:
class bignum
{
public:
// Constructors.
bignum();
bignum(int num_digits);
bignum(const string &digits);
bignum(const bignum &other);
// Destructors.
~bignum();
// Assignment operator.
bignum &operator=(const bignum &other);
// Accessors
int digits() const;
int as_int() const;
string as_string() const;
void print(ostream &out) const;
bignum add(const bignum &other) const;
bignum multiply(const bignum &other) const;
bool equals(const bignum &other) const;
int PublicNumberTest;
private:
// Pointer to a dynamically-allocated array of integers.
int *digit;
// Number of digits in the array, not counting leading zeros.
int ndigits;
};
#endif
and here is one of my member functions:
bignum bignum::multiply(const bignum& other) const{
bignum product;
bignum row;
int carry = 0;
int sum = 0;
int j = 0;
int *temp_row = new int[];
for (int i = 0; i < ndigits-1; i++){
carry = 0;
temp_row[i] = 0;
for (j; j < other.digits - 1; j++){
sum = digit[i] * other.digit[j] + carry;
temp_row[i + j] = sum % 10;
carry = sum / 10;
}
if (carry>0)
temp_row[i + j] = carry;
row = row operator+temp_row //This is what I don't understand. How can I
product = product.add(row); //assign the contents of temp_row?
}
}
There is another, but it is basically the same problem. I have an array that I would like to copy to the contents of and place inside of my...class? I guess? Thanks for reading.
If you would like to use temp_row[]
as the set of digits for this
object, you could do:
delete [] digits;
digits = new_row;
You should probably update ndigits
as well.
2 ways of doing that: The "old" way, which should be very similar to calling the destructor then the copy constructor:
bignum& bignum::operator=(const bignum& other)
{
delete[] digit;
ndigits = other.ndigits;
digit = new int[ndigits];
for (int i = 0; i != ndigits; ++i) {
digit[i] = other.digit[i];
}
return *this;
}
code which can be also written as:
bignum& bignum::operator=(const bignum& other)
{
this->~bignum();
new(this) bignum(other);
return *this;
}
This old way is now discouraged because not exception safe.
The "new" way, which is exception safe: you define a swap() member method that never throws any exception and use it to implement the operator=():
void bignum::swap(bignum& other) noexcept
{
std::swap(ndigits, other.ndigits);
std::swap(digit, other.digit);
}
bignum& bignum::operator=(const bignum& other)
{
bignum tmp(other);
this->swap(tmp);
return *this;
}
Rather than work out your multiple digit multiplication, I'm just leaving a couple of things to puzzle out,
Do you want to perform c = a b or a = a b? that is, do you intend to multiply two arguments, produce a temporary, and assign it to a third, or do you plan to just multiply the object a by an argument b?
My intention was C = A*B
Then you need to allocate (new) a destination bignum,
Use the friend operator overload should solve your need; it is common idiom to define binary operators (note that I didn't solve your allocating the right number of digits),
friend bignum operator+(const bignum& a, const bignum& b); //in class
//you want to calculate A * B --> C
//you need a temporary place to calculate C, then produce a new C,
bignum bignum::multiply( const bignum& A, const bignum& B ) const
{
//Your A is this, A.digit[];
//Your B is other, B.digit[];
//Yout C is temp, a buffer for calculations,
bignum temp;
int carry = 0;
int sum = 0;
int j = 0;
//you don't need this, you have temp.digit[];
for (int i = 0; i < this->ndigits-1; i++)
{
carry = 0;
temp.digit[i] = 0;
//what do you want to initialize j to?
for (j; j < B.ndigits - 1; j++)
{
sum = A.digit[i] * B.digit[j] + carry;
temp.digit[i + j] = sum % 10;
carry = sum / 10;
}
if (carry>0)
temp.digit[i + j] = carry;
}
return bignum(temp); //allocate a new bignum
}
See here for an example of how to overload a binary operator...
Doing the way you did has drawbacks (returns a const, can't chain more than two additions togethere), but basically you do something similar, but use the (this->)
pointer,
//you want to calculate A * B --> C
//you need a temporary place to calculate C, then produce a new C,
bignum bignum::multiply(const bignum& other) const
{
//Your A is this, this->digit[];
//Your B is other
//Yout C is temp product, a buffer for calculations,
bignum temp;
int carry = 0;
int sum = 0;
int j = 0;
for (int i = 0; i < this->ndigits-1; i++)
{
carry = 0;
temp.digit[i] = 0;
for (j; j < other.ndigits - 1; j++)
{
sum = this->digit[i] * other.digit[j] + carry;
temp.digit[i + j] = sum % 10;
carry = sum / 10;
}
if (carry>0)
temp.digit[i + j] = carry;
}
//bignum product;
//return *this = temp; //do this,
return bignum(temp); //or you should probably return a new object
}
you should probably return a new object .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.