Skip to main content
Add tags and make title more descriptive
Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308

Operator Overloading Program Simple String class with operators

Source Link

Operator Overloading Program

Any input is appreciated! And can I get some advice on how to properly delete the dynamically allocated memory with the destructor? My professor said we have to delete each individual element of an array before doing delete [] array, and I'm not sure how I would do that here because I don't know the length of the array.

These are the instructions for the assignment http://voyager.deanza.edu/~bentley/cis22b/ass8.html

#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) const;
    bool operator > (const String& obj) const;
    bool operator == (const String& obj) const;
    void operator = (const String& obj);
    void operator += (const String& obj);
    char* operator + (const String& obj) const;
    int operator ! () const;
    char operator[] (int) const;
    char* operator * () const
    {
        return data;
    }
    friend ostream & operator << (ostream&, String&);

};

String::String()
    {
        data = new char[0];
        data[0] = '\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) const
    {
        if(strcmp(data, obj.data) < 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

void String::countA()
    {
        for(int i = 0; data[i] != '\0'; i++)
        {
            if(data[i] == 'a' || data[i] == 'A')
                {
                    numA++;
                }
        }
    }


void String::lowerNumA()
    {
        for(int i = 0; data[i] != '\0'; i++)
        {
            if(data[i]=='a' || data[i]=='A')
            {
                numA--;
            }
        }
    }


bool String::operator > (String const &obj) const
    {
        if(strcmp(data, obj.data) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

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

        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] != '\0')
        {
            element++;
        }

        int element2 = 0;

        while(obj.data[element2] != '\0')
        {
            data[element] = obj.data[element2];
            element++;
            element2++;
        }

        data[element]='\0';
        countA();
    }

char* String::operator + (String const &obj) const
    {
        char* newChar = new char[100];
        int newCount = 0;
        int index = 0;

        while(data[index]!='\0')
        {
            newChar[newCount]= data[index];
            newCount++;
            index++;
        }

        index = 0;

        while(obj.data[index] != '\0')
        {
            newChar[newCount] = obj.data[index];
            newCount++;
            index++;
        }

        newChar[newCount] = '\0';

        cout << "";
        
        return newChar;

    }

int String::operator ! () const
    {
        int index = 0;
        int size = 0;

        while(data[index] != '\0')
        {
            size++;
            index++;
        }

        return size;
    }

char String::operator[] (int inIndex) const
    {
        int index = 0;
        while(data[index] != '\0')
        {
            index++;
        }

        if(inIndex >= 0 && inIndex <= index)
        {
            return data[inIndex];
        }
        else
        {
            cout<<"out of bound\n";
            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] != '\0')
    {
        out << things.data[index];
        index++;
    }

    return out;
}