Salve a tutti,
qualcuno potrebbe aiutarmi a capire il seguente codice?
Il codice è preso da un esercizio del manuale DEITEL&DEITEL sulla ridefinizione degli operatori. La classe dovrebbe gestire numeri interi di grandi dimensioni. Il membro della classe è il vettore integer deputato a contenere il numero di grandi dimensioni.
Da come ho capito fino ad ora, mi sarebbe bastato utilizzare solamente la prima dichiarazione (quella che ha come argomento un altro oggetto della classe HugeInt) delle tre che ridefiniscono l'operatore di addizione; Infatti nel caso che il secondo operando fosse stato di tipo int o char* sarebbe stato chiamato il costruttore di conversione che avrebbe convertito il tipo nativo nell'oggetto della classe.
Il ragionamento è corretto o mi sfugge qualcosa?
codice:
/*INTEFACCIA DELLA CLASSE File HugeInt.h*/
#ifndef HUGEINT_H
#define HUGEINT_H
#include <iostream>
using std::ostream;
class HugeInt
{
friend ostream& operator<< (ostream&, const HugeInt&);
public:
HugeInt(long = 0); //Costruttore di default-conversione
HugeInt(const char*); //Costruttore di conversione per numeri molto grandi
//Ridefinizione operatore di addizione
HugeInt operator+ (const HugeInt&) const;
HugeInt operator+ (int) const; //---------->Non necessaria se utilizzo costruttore di conversione
HugeInt operator+ (const char*) const; //-------->Non necessaria se utilizzo costr. di conversione
private:
static const int size = 30;
short integer[size];
};
#endif
/*DEFINIZIONE DELLA CLASSE File HugeInt.cpp*/
#include <iostream>
using std::cout;
using std::endl;
#include <cstring>
using std::isdigit;
#include "HugeInt.h"
HugeInt::HugeInt ( long value )
{
...
//Costruttore di default-conversione
}
HugeInt::HugeInt (const char* string)
{
...
//Costruttore di conversione
}
//Dovrebbe essere suffciente anche per le operazone con tipi int e string
HugeInt HugeInt::operator+ (const HugeInt& op2) const
{
int limit = size - 1;
int resto = 0;
HugeInt temp;
for (int j = limit; j >= 0; j--)
{
temp.integer[j] = this->integer[j] + op2.integer[j] + resto;
if (temp.integer[j] > 9)
{
resto = 1;
temp.integer[j] %= 10;
}
else resto = 0;
}
return temp;
}
//Potrebbe non essere necessario
HugeInt HugeInt::operator+ (int op2) const
{
return *this + HugeInt(op2);
}
//Potrebbe non essere necessario
HugeInt HugeInt::operator+ (const char* strOp2) const
{
return *this + HugeInt(strOp2);
}
Grazie a tutti