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);
}