Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

I'm not sure how to use a destructor to delete dynamically allocated array.

I'm not sure how to use a destructor to delete dynamically allocated array. My professor said we have to delete each individual element of the array before deleting the whole array otherwise it would cause a memory leak. But I'm not sure how I would go about doing that with my program.

#include <iostream>

#include <cstring>

using namespace std;

class String

{

private:

  char* data;

  static int numA;

public:

  String();

  String(const string&);

  String(const String&);

  ~String();

  static int a_count()

  {

    return numA;

  }

  void countA();

  void lowerNumA();

  bool operator < (const String& obj);

  bool operator > (const String& obj);

  bool operator == (const String& obj);

  void operator = (const String& obj);

  void operator += (const String& obj);

  char* operator + (const String& obj);

  int operator ! ();

  char operator[] (int inIndex);

  char* operator * ()

  {

    return data;

  }

  friend ostream & operator << (ostream&, String&);

};

String::String()

  {

    data = new char[0];

    data[0] = '';

    countA();

  }

String::String(const string& thing)

  {

    data = new char[0];

    strcpy(data, thing.c_str());

    countA();

  }

String::String(const String &s2)

  {

    data = s2.data;

    countA();

  }

String::~String()

{

  delete [] data;

}

bool String::operator < (String const &obj)

  {

    if(strcmp(data, obj.data) < 0)

    {

      return true;

    }

    else

    {

      return false;

    }

  }

void String::countA()

  {

    for(int i = 0; data[i] != ''; i++)

    {

      if(data[i] == 'a' || data[i] == 'A')

        {

          numA++;

        }

    }

  }

void String::lowerNumA()

  {

    for(int i = 0; data[i] != ''; i++)

    {

      if(data[i]=='a' || data[i]=='A')

      {

        numA--;

      }

    }

  }

bool String::operator > (String const &obj)

  {

    if(strcmp(data, obj.data) > 0)

    {

      return true;

    }

    else

    {

      return false;

    }

  }

bool String::operator == (String const &obj)

  {

    if(strcmp(data, obj.data) == 0)

    {

      return true;

    }

    else

    {

      return false;

    }

  }

void String::operator = (String const &obj)

  {

    lowerNumA();

    data = obj.data;

    countA();

  }

void String::operator += (String const &obj)

  {

    lowerNumA();

    int element = 0;

    while(data[element] != '')

    {

      element++;

    }

    int element2 = 0;

    while(obj.data[element2] != '')

    {

      data[element] = obj.data[element2];

      element++;

      element2++;

    }

    data[element]='';

    countA();

  }

char* String::operator + (String const &obj)

  {

    char* newChar = new char[100];

    int newCount = 0;

    int index = 0;

    while(data[index]!='')

    {

      newChar[newCount]= data[index];

      newCount++;

      index++;

    }

    index = 0;

    while(obj.data[index]!='')

    {

      newChar[newCount]= obj.data[index];

      newCount++;

      index++;

    }

    newChar[newCount] = '';

    cout << "";

    return newChar;

  }

int String::operator ! ()

  {

    int index = 0;

    int size = 0;

    while(data[index] != '')

    {

      size++;

      index++;

    }

    return size;

  }

char String::operator[] (int inIndex)

  {

    int index=0;

    while(data[index] != '')

    {

      index++;

    }

    if(inIndex >= 0 && inIndex <= index)

    {

      return data[inIndex];

    }

    else

    {

      cout<<"out of boundn";

      return ' ';

    }

  }

int String::numA;

int main()

{

  // Constructors

  String A("apple");

  String B("banana");

  String C("cantaloupe");

  String D(B);

  String E;

  // static member function

  cout << "Number of a's = " << String::a_count() << endl << endl;

  // Overloaded insertion operator

  cout << "A = " << A << endl;

  cout << "B = " << B << endl;

  cout << "C = " << C << endl;

  cout << "D = " << D << endl;

  cout << "E = " << E << endl << endl;

  // Relational operators

  cout << boolalpha;

  cout << "A < B " << (A < B) << endl;

  cout << "B < A " << (B < A) << endl;

  cout << "A == B " << (A == B) << endl << endl;

  // Assignment operator

  A = B;

  cout << "A = " << A << endl;

  cout << "A == B " << (A == B) << endl << endl;

  // Size (bang) operator

  cout << "A size = " << !A << endl;

  cout << "E size = " << !E << endl << endl;

  // Unary * operator

  cout << "C text = " << *C << endl << endl;

  // Plus operator

  cout << "A + B = " << A + B << endl << endl;

  // Plus equal operator

  A += C;

  cout << "A = " << A << endl << endl;

  // Index operator

  cout << "A[3] = " << A[3] << endl << endl;

  // static member function

  cout << "Number of a's = " << String::a_count() << endl;

}

ostream & operator << (ostream& out, String& things)

{

  int index = 0;

  while(things.data[index] != '')

  {

    out << things.data[index];

    index++;

  }

  return out;

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question