Im trying a practice problem from a textbook and here is my attempt at it, was wondering if I could get some advice both with structurally how to approach the problem, efficency, and any words of wisdom on dynamic memory. This is my study for my final exam which so any comments will be greatly appreciated.

Problem:
Create a class with a dynamic array of unsigned char holding 0-9 that can make an arbitary large integer. Create operator overloading functions to pair.

Code:

``````//LongInt.h

#ifndef _LONGINT_H
#define _LONGINT_H

class LongInt
{
public:
LongInt(int number, int size); //constructor
~LongInt();                    //destructor

private:
int IntSize;                   //holds size of array
char[]* LongArr;                //dynamic array of integers 0-9
int getInt();                  //turns array into int

};

#endif``````
``````//LongInt.cpp

#include "LongInt.h"

LongInt::LongInt(number, size)
{
IntSize = size;
LongArr = new unsigned char* [IntSize];
numStr = new string(number)
for(int i=0; i<IntSize; i++){ *LongArr[i] = numStr[i]; }
}

LongInt::~LongInt()
{
for(int i=0; i<IntSize; i++){ *LongArr = 0; }
}

int LongInt::getInt()
{
s = new string;
for(int i=0; i<IntSize; i++) { s.append(LongArr[i]); }
delete s;

i = new int(s);
return i;
}

LongInt operator+(const LongInt &L1, const LongInt &L2)
{
new int* int1  = L1.getInt();
new int* int2  = L2.getInt();
new int newInt = *int1 + *int2;

return LongInt(newInt);
}``````

## All 4 Replies

Have you tried compiling it? There's quite a few syntactical liberties in there. :)

``char[]* LongArr;``

is not valid syntax.

Where does numStr in the constructor come from?

Where does i come from in `getInt()` ?

``new int* int1``

new doesn't belong on the left side of that expression

I think some of the ideas are starting to come together, but study some examples from your text or otherwise and take another crack at it.

You never actually stated a question...??? What do you actually need help with? If you post code you also need a question to go along with it.

What would be the basic syntax or code to create a dynamic array of characters where each pointed to an integer?

char * longInt = new char[x];

Now longInt is a char pointer pointing to the first element of a container of x number of elements of type char. That means that longInt acts like an array, though technically it isn't an array, but that is a discussion I still don't understand completely. The number x must, in the end analysis, end up being a positive integer, but it could be any positive integer from 1 to the maximum value of type int.

Now in order for longInt to be used like a number you need to be able to convert each element in longInt from a char/digit to an int and back again. That will be another step in your program.

Then in order to do things like add two "longInts" together you need to be able to "carry" a one to another element in the array if the sum of the two "characters" with the same index in each of the two arrays is over 9.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.