自己实现String类

Implement a string class in C++ with basic functionality like comparison, 
concatenation, input and output. Please also provide some test cases and using scenarios (sample code of using this class).
Please do not use MFC, STL and other libraries in your implementation.
我的实现方案如下,这道题真地对c++的主要特性都进行了较好地考察. 

1 引用与指针。

2 类,构造,析构,拷贝构造函数。

3 运算符重载。

4 友元。类外的非成员函数访问类的私有成员。 

1 MyString.h 

#ifndef STRING_H_INCLUDED #define STRING_H_INCLUDED #include<iostream> using namespace std; class String { private: char* chars; public: //构造函数 String(); String(const char*);//用c字符串初始化 String(const String&);//用String初始化。拷贝构造函数 //析造函数 ~String(); char& operator[](int i)const; //字符串赋值 //不用考虑运算符对称性,所以使用成员函数。并且采用by adress返回方式,因为修改的是自己。返回String& String& operator = (const String&); String& operator += (const String&); //字符串拼接 //考虑到运算符对称性,使用友元的非成员函数。并且采用by value返回方式。返回String friend String operator+(const String&, const String &); //字符串比较。考虑到运算符对称性,使用友元的非成员函数。 friend bool operator==(const String&, const String&); friend bool operator!=(const String&, const String&); friend bool operator>(const String&, const String&); friend bool operator>=(const String&, const String&); friend bool operator<(const String&, const String&); friend bool operator<=(const String&, const String&); //输出,必须是友元的非成员函数,因为第一个参数不是this。第二个参数是const friend ostream & operator<<(ostream&, const String&); //输入,必须是友元的非成员函数,因为第一个参数不是this。第二个参数不是const friend istream & operator>>(istream&, String&); //其他 int length()const;//返回当前字符串的长度 bool empty()const;//当前字符串是否为空 }; #endif // STRING_H_INCLUDED  

如何正确的设计运算符重载以及友元,请参见http://blog.csdn.net/HEYUTAO007/archive/2010/09/04/5863364.aspx

2 MyString.cpp 

#include"MyString.h" #include<cstring> #include<cassert> //构造函数 String::String() { chars = new char[1]; chars[0] = '/0'; } String::String(const char* s) { if(s == NULL) { chars = new char[1]; chars[0] = '/0'; } else { chars = new char[strlen(s)+1]; strcpy(chars,s); } } String::String(const String& s) { chars = new char[strlen(s.chars)+1]; strcpy(chars,s.chars); } //析造函数 String::~String() { delete[] chars; chars = NULL; } char& String::operator[](int i)const { assert(i<=strlen(chars)); return chars[i]; } //字符串赋值 String& String::operator = (const String& s) { if(this == &s) return *this; delete[] chars; chars = new char[strlen(s.chars)+1]; strcpy(chars,s.chars); return *this; } String& String::operator += (const String& s) { int len = strlen(chars); char *tmp = new char[len + strlen(s.chars)+1]; strcpy(tmp,chars); strcpy(tmp + len,s.chars); delete[] chars; chars = tmp; return *this; } //字符串拼接。友元实现,没有String::。 String operator + (const String& s1, const String& s2) { int len = strlen(s1.chars); char* chars = new char[len + strlen(s2.chars) +1]; strcpy(chars,s1.chars); strcpy(chars + len,s2.chars); String tmp; delete[] tmp.chars; tmp.chars = chars; return tmp; } //字符串比较 bool operator == (const String& s1, const String& s2) { return !strcmp(s1.chars, s2.chars); } bool operator!=(const String& s1, const String& s2) { return strcmp(s1.chars, s2.chars); } bool operator < (const String& s1, const String& s2) { return strcmp(s1.chars, s2.chars) < 0; } bool operator <= (const String& s1, const String& s2) { return strcmp(s1.chars, s2.chars) <= 0; } bool operator > (const String& s1, const String& s2) { return strcmp(s1.chars, s2.chars) > 0; } bool operator >= (const String& s1, const String& s2) { return strcmp(s1.chars, s2.chars) >= 0; } //输出 ostream& operator<<(ostream& cout, const String& s) { cout<<s.chars; return cout; } //输入 istream& operator>>(istream& cin, String& s) { char str[100000]; cin >> str; delete[] s.chars; s.chars = new char[strlen(str)+1]; strcpy(s.chars,str); return cin; } //其他 int String::length()const//返回当前字符串的长度 { return strlen(chars); } bool String::empty()const//当前字符串是否为空 { return !strlen(chars); } int main() { String a("first"); String b(a); String c = a; cout<<a<<endl; cout<<b<<endl; cout<<c<<endl; cout<<(a==b)<<endl; cout<<a[2]<<endl; c+=a; cout<<c<<endl; String d; cin>>d; cout<<d<<endl; b = a+d; cout<<b<<endl; cout<<a.length()<<endl; cout<<a.empty()<<endl; return 0; }

 /* 运行结果:

first
first
first
1
r
firstfirst
second
second
firstsecond
5
0
*/

你可能感兴趣的:(String,basic,delete,Class,mfc,concatenation)