Create string class based on char array - C++ Class

C++ examples for Class:Operator Overload

Description

Create string class based on char array

                                                                        
#include <iostream> 
#include <string.h> 
                                                                         
class String 
{ 
public: 
    // constructors 
    String(); 
    String(const char *const); 
    String(const String&); 
    ~String(); 
                                                                         
    // overloaded operators 
    char& operator[](int offset); 
    char operator[](int offset) const; 
    String operator+(const String&); 
    void operator+=(const String&); 
    String& operator= (const String &); 
                                                                         
    // general accessors 
    int getLen() const { return len; } 
    const char* getString() const { return value; } 
    // static int constructorCount; 
                                                                         
private: 
    String(int); // private constructor 
    char* value; 
    int len; 
}; 
                                                                         
// default constructor creates string of 0 bytes 
String::String() 
{ 
    value = new char[1]; 
    value[0] = '\0'; 
    len = 0; 
    // std::cout << "\tDefault string constructor\n"; 
    // constructorCount++; 
} 
                                                                         
String::String(int len) 
{ 
    value = new char[len + 1]; 
    int i; 
    for (i = 0; i < len; i++) 
         value[i] = '\0'; 
    len = len; 
    // std::cout << "\tString(int) constructor\n"; 
    // constructorCount++; 
} 
                                                                         
String::String(const char* const cString) 
{ 
    len = strlen(cString); 
    value = new char[len + 1]; 
    int i; 
    for (i = 0; i < len; i++) 
         value[i] = cString[i]; 
    value[len] = '\0'; 
    // std::cout << "\tString(char*) constructor\n"; 
    // constructorCount++; 
} 
                                                                         
String::String(const String& rhs) 
{ 
    len = rhs.getLen(); 
    value = new char[len + 1]; 
    int i; 
    for (i = 0; i < len; i++) 
         value[i] = rhs[i]; 
    value[len] = '\0'; 
    // std::cout << "\tString(String&) constructor\n"; 
    // constructorCount++; 
} 
                                                                         
String::~String() 
{ 
    delete [] value; 
    len = 0; 
    // std::cout << "\tString destructor\n"; 
} 
                                                                         
String& String::operator=(const String &rhs) 
{ 
    if (this == &rhs) 
         return *this; 
    delete [] value; 
    len = rhs.getLen(); 
    value = new char[len + 1]; 
    int i; 
    for (i = 0; i < len; i++) 
             value[i] = rhs[i]; 
    value[len] = '\0'; 
    return *this; 
    // std::cout << "\tString operator=\n"; 
} 
                                                                         
char& String::operator[](int offset) 
{ 
    if (offset > len) 
        return value[len - 1]; 
    else 
        return value[offset]; 
} 
                                                                         
char String::operator[](int offset) const { 
    if (offset > len) 
        return value[len-1]; 
    else 
        return value[offset]; 
} 
                                                                         
String String::operator+(const String& rhs) { 
    int totalLen = len + rhs.getLen(); 
    int i, j; 
    String temp(totalLen); 
    for (i = 0; i < len; i++) 
             temp[i] = value[i]; 
    for (j = 0; j < rhs.getLen(); j++, i++) 
             temp[i] = rhs[j]; 
    temp[totalLen] = '\0'; 
    return temp; 
} 
                                                                         
// changes current string, returns nothing 
void String::operator+=(const String& rhs) 
{ 
    int rhsLen = rhs.getLen(); 
    int totalLen = len + rhsLen; 
    int i, j; 
    String temp(totalLen); 
    for (i = 0; i < len; i++) 
           temp[i] = value[i]; 
    for (j = 0; j < rhs.getLen(); j++, i++) 
           temp[i] = rhs[i - len]; 
    temp[totalLen] = '\0'; 
    *this = temp; 
} 
                                                                         
// int String::constructorCount = 0;

Related Tutorials