Like I said, use header files correctly.
I'd also use C++ strings, but I don't know if you are prohibited to do that or anything. C++ strings have their own memory management, which is ideal.
main.cpp:
#include "stash.h"
int main()
{
stash o("hello world");
stash b;
b = o;
o.show();
b.show();
return 0;
}
stash.cpp
#include "stash.h"
int gIncrement = 4;
//**constructors and destructors
stash::stash(){
ch = new char [0];
currentSize = 0;
currentStorage = 0;
next = 0;
}
stash::stash(int startSize){
ch = new char [startSize];
currentSize = 0;
currentStorage = startSize;
next = currentStorage - currentSize;
}
stash::~stash(){
delete [] ch;
std::cout << "Freeing storage\n";
}
stash::stash(char *p){
clearStash();
insertString(p);
}
stash::stash(stash& other){
clearStash();
insertString(other.returnPointerToCh());
}
//**
//--Increase the stash's size to hold more chars.
void stash::inflate(){
int i;
char *temp = new char [currentSize + gIncrement];
for(i=0; i<currentSize; i++)
temp[i] = ch[i];
delete [] ch;
ch = temp;
currentStorage = currentStorage + gIncrement;
}
//--add a character to the stash and resize it according
//--to space needed
void stash::add(char p){
if(next == 0)
inflate();
ch[currentSize] = p;
++currentSize;
next = currentStorage - currentSize;
}
//--reset the stash
void stash::clearStash(){
currentSize = 0;
currentStorage = 0;
ch = new char [0];
next = currentStorage - currentSize;
}
//--insert an entire string into the stash
void stash::insertString(char *p){
int i;
for(i=0; p[i]; i++){
add(p[i]);
}
}
//--make the chars a string
char* stash::returnAsString(){
add('\0');
next--; //to overwrite the zero byte next time we add something
return ch;
}
//--add more chars into the string;
void stash::operator+(char *p){
insertString(p);
}
//--Return a character at a specified position
//--Return NULL if a character doesn't exist at the
//--requested position
char stash::returnCharacterAtPosition(int i){
if(i>currentSize)
return NULL;
return ch[i];
}
//--clear the stash and hold a new string
void stash::operator=(char *p){
clearStash();
insertString(p);
}
//--THIS HAS PROBLEMS WITH MEMORY ALLOCATION!!!
void stash::operator=(stash &s){
int i;
delete [] ch;
char *temp = new char [s.returnCurrentSize()];
for(i=0; i<s.returnCurrentSize(); i++){
temp[i] = s.returnCharacterAtPosition(i);
}
next = s.returnNext();
currentStorage = s.returnCurrentStorage();
currentSize = s.returnCurrentSize();
ch = temp;
}
stash.h
#ifndef STASH_H
#define STASH_H
#include <iostream>
class stash{
char *ch;
int currentSize;
int currentStorage;
int next;
public:
stash();
stash(int startSize);
stash(char *p);
stash(stash& other);
~stash();
void show(){
std::cout << "currentStorage: " << currentStorage << std::endl;
std::cout << "current size: " << currentSize << std::endl;
std::cout << "Next: " << next << std::endl;
for(int i=0; i<currentSize; i++)
std::cout << ch[i];
std::cout << std::endl;
}
void inflate();
void add(char p);
void insertString(char *p);
char* returnAsString();
void clearStash();
char returnCharacterAtPosition(int i);
int returnCurrentSize(){return currentSize;}
int returnCurrentStorage(){return currentStorage;}
int returnNext(){return next;}
char* returnPointerToCh(){return ch;}
void operator+(char *p);
void operator=(char *p);
void operator=(stash &s);
};
#endif