Click to See Complete Forum and Search --> : data type using classes and overloaded operators.


belle_18
January 19th, 2003, 06:15 AM
hi!
I've done the assignment, just need someone to double check and see if its ok with the spec....

thanx!
----------------------------------------------------------------------

Aim : To develop a new data type using classes and overloaded operators.

Process:
The size of integers in C++ is a limitation when we need to manipulate integers of arbitrary number of digits. In this assignment we will develop a datatype to be able to handle addition involving unsigned very long integers.

The interface file UVLI.h contains

struct Digit
{
char d;
Digit* next;
};
typedef Digit* DPtr;

And the following class defination;

class UVLI
{
friend ostream& operator << (ostream&. const UVLI&);
//read a sequence of characters as a UVLI
friend istream& operator >>(istream&. const UVLI&);
//write a sequence of characters as UVLI

public:
UVLI(); //default costructor set to 0 digits.
UVLI(const UVLI&); //copy constructor - deep copy
~UVLI(); //reset and clean up memory
UVLI& opeartor+(const UVLI&); //add two UVLI's together
UVLI& operator=(const UVLI&); /assigns one UVLI to another

bool opeator==(const UVLI&) const;
//are to UVLIs equal?
bool operator!=(const UVLI&) const; //are two UVLIs not equal?

void UVLIReverse(); //reverse the digits
void long2UVLI(insigned long); //convert long to UVLI
double UVLI2double(const UVLI&); //convert UVLI to double

private:
int nodigits; //no of digits in UVLI
DPtr first; //pointer to list of chars
};

Thus an unsigned very long integer (UVLI) is defined as nodigits connected as a list. Space for each digit will be dynamically allocated and linked.

The constructor UVLI() sets the number of digits and both pointers to 0. This is the representation of 0 for UVLIs. This must be the first function used for all new UVLIs. The other constructor UVLI(const UVLI&); is a copy constructor which copies one UVLI to another. It should free any resources in the target and perform a deep copy.

~UVLI() is the destructor. IT does the same as the default constructor but frees up any space already allocated prior to setting to 0. It is called automatically when the object terminates.

UVLI& operator=(const UVLI&); assigns the value of the second argument into the first ie. A=B As with all functions which produce a new value, the current contents of the resultant must be cleared. Again it is a deep copy.

The function UVLI& operator+(const UVLI&), is used to take two UVLI's and add them together. It returns an object of type UVLI which can be assigned. The function would be used in the following context implicity ie. A+B. Work from least significant to most significant digit, utilising the reversing function. Dont forget that the two operandsin the + may be of different lengths.

The two functions:

friend ostream& operator << (ostream&. const UVLI&);
friend istream& operator >>(istream&. const UVLI&);

are for input and output of these UVLIs. Note the use of istream and ostream, so that the routines can be used on files as well as the defaults streams. Assume that the input involves no errors. For example, reading a UVLI, the digits appear from left to right. Just keep reading one digit at a time, until a non-digit is read. Leading zero's should be ignored.

bool operator==(const UVLI&) const; and bool operator!=(const UVLI&) const are used to check to see if two UVLI's used in such expressions are equal or not equal. The overloaded operator should return true or false. The operator returns a boolean on some compiliers this may have to be changed to an integer,

UVLI2double() is a conversion function for changing a UVLI to a double (useful for testing).

UVLIReverse() might seem a strange function to create for integer, but it makes the other function easier. This function reverse the digits in a UVLI. This means the next links are reversed and the first digit is now the least significant one of the original integer. This may be used for input or addition.

long2UVLI() is another conversion routine to take a long integer and store it as a UVLI. By working from least significant to most significant digit and then reversing the result, this task becomes simpler.

Refer to example source file could uvli_main.cpp. This has extra comments which describes the operation of your code.

Place the code for the above class declaration in the file uvli.cpp. Write a header file containing the class declaration called uvli.h which is included in the program. Do not add any extra declarations, method or types.



--------------------------------------------------------------------------------
//uvli.h


/*
Header for UVLI class
*/

struct Digit
{
char d;
Digit* next;
};

typedef Digit* DPtr;

class UVLI
{
friend ostream& operator<<(ostream&. const UVLI&); // read a sequence of
// characters as a UVLI
friend istream& operator>>(istream&. const UVLI&); // write a sequence of
// characters as UVLI
public:
UVLI(); // default constructor set to 0 digits
UVLI(const UVLI&); // copy constructor - deep copy
~UVLI(); // reset and clean up memory
UVLI& operator+(const UVLI&); // add two ULVIÕs together
UVLI& operator=(const UVLI&); // assigns one ULVI to another
bool operator==(const UVLI&) const; // are two UVLIs equal?
bool operator!=(const UVLI&) const; // are two UVLIÕs not equal
void UVLIReverse(); // reverse the digits
void long2UVLI(unsigned long); // convert long to UVLI
double UVLI2double(const UVLI& ); // convert UVLI to double
private:
int nodigits; // no of digits in UVLI
DPtr first; // pointer to list of chars
};


--------------------------------------------------------------------------------

uvli_main.cpp

#include <iostream>
#include "uvli.h"
using namespace std;

/*
When ever over writing the UVLI with data you should check to see if the private members already have
data. If so you should remove any preexsiting data.
*/

int main()
{
UVLI instance1;
UVLI instance2;

cin >> instance1; // enter a value such as 1234564632323243535 on one line - get till newline
cin >> instance2;

if (instance1 == instance2)
{
cout << "instance1 and instance2 are the same" << endl;
}

if (instance1 != instance2)
{
cout << "instance1 and instance2 are NOT the same" << endl;
}

UVLI instance3(instance2); // copy constructor
UVLI instance4 = instance3; // copy constructor

instance1 = instance2; // assign UVLI instance2 to instance1, be sure to free any resource
// previously in instance1

long number;
cin >> number;

instance1.long2UVLI(number); // convert long to UVLI, be careful with memory and the fact instance1
// may already have a UVLI in it. You should handle this. Delete preexsiting
// data

instance3 = instance1 + instance2; // tests + operator and = operator. Be sure to check for memory.

cout << instance3 << endl;

return 0;
}

---------------------------------------------------------------------------

Paul McKenzie
January 19th, 2003, 09:12 AM
we will develop a datatype to be able to handle addition involving unsigned very long integersIt doesn't handle all additions. Missing is operator += and operator ++ (post and pre-increment).

Also does your assignment operator (operator =) check for self-assignment? For example, does it look like this:

UVLI & UVLI::operator=(const UVLI& rhs)
{
if ( this == &rhs )
return *this;
// rest of code
}

If not, then what do you think would happen if this occurs:

UVLI instance;
instance = instance;

Regards,

Paul McKenzie