String.h
#pragma once #include <iostream> using namespace std; #include <string.h> class String { friend ostream& operator<<(ostream& out, String& obj); friend istream& operator>>(istream& in, String& obj); friend bool operator <(const String& left, const String& right); friend bool operator >(const String& left, const String& right); friend bool operator ==(const String& left, const String& right); friend bool operator !=(const String& left, const String& right); public: String(); String(int n, char c); String(const char* source); String(const String& obj); String& operator=(char* s); String& operator=(const String& obj); ~String(); char& operator[](int i); const char& operator[](int i)const; String& operator+=(const String& obj); String& operator+=(const char* s); char* getData() { return data; } int length() { return size; } private: int size; char* data; };
String.cpp
#include "stdafx.h" #include "String.h" String::String() { data = new char[1]; *data = '\0'; size = 0; } String::String(int n, char c) { data = new char[n + 1]; size = n; char *temp = data; while (n--) { *temp = c; temp++; } temp = '\0'; } String::String(const char* source) { if (source == nullptr) { data = new char[1]; *data = '\0'; size = 0; } else { size = strlen(source); data = new char[size + 1]; strcpy(data, source); } } String::String(const String& obj) { data = new char[obj.size + 1]; strcpy(data,obj.data); size = obj.size; } String& String::operator=(char* s) { if (data != nullptr) { delete[] data; } size = strlen(s); data = new char[size + 1]; strcpy(data, s); return *this; } String& String::operator=(const String& obj) { if (this == &obj) { return *this; } if (data != nullptr) { delete[] data; } size = obj.size; data = new char[size + 1]; strcpy(data, obj.data); return *this; } String::~String() { } char& String::operator[](int i) { return data[i]; } const char& String::operator[](int i)const { return data[i]; } String& String::operator+=(const String& obj) { int len = size + obj.size + 1; char * temp = data; data = new char[len]; size = len - 1; strcpy(data, temp); strcat(data, obj.data); delete[] temp; return *this; } String& String::operator+=(const char* s) { if (s == nullptr) { return *this; } int len = size + strlen(s) + 1; char* temp = data; data = new char[len]; size = len - 1; strcpy(data, temp); strcat(data, s); delete[] temp; return *this; } ostream& operator<<(ostream& out, String& obj) { for (int i = 0; i < obj.length(); ++i) { out << obj[i] << endl; } return out; } istream& operator>>(istream& in, String& obj) { char p[1024]; in.getline(p, 1024); obj = p; return in; } bool operator <(const String& left, const String& right) { int i = 0; while (left[i] == right[i] && left[i] != 0 && right[i] != 0) { i++; } return left[i] - right[i] < 0 ? true : false; } bool operator >(const String& left, const String& right) { int i = 0; while (left[i] == right[i]&&left[i] != 0 && right[i] != 0) { i++; } return left[i] - right[i]>0 ? true : false; } bool operator ==(const String& left, const String& right) { int i = 0; while (left[i] == right[i] && left[i] != 0 && right[i] != 0) { i++; } return left[i] - right[i]==0 ? true : false; } bool operator !=(const String& left, const String& right) { int i = 0; while (left[i] == right[i] && left[i] != 0 && right[i] != 0) { i++; } return left[i] - right[i]!=0 ? true : false; }