第6章类与对象 例62设计一个简单的字符串类,类中有两个数据成员,分别 表示字符串的长度和字符串的内容,有一个构造函数和一个析 构函数,函数 GetLength()返回字符串长度,函数 GetContents()获得字符串的内容,重载函数 SetContents( 字符串设置值。 include <iostream .h> include <string h> class CString private int length; char *contents public CString0;∥构造函数 CString0;∥析构函数 int Getlength 0; void Getcontents(char *str); void SetContents(int len, char *cont) void Setcontents(char *cont); }
例6.2 设计一个简单的字符串类,类中有两个数据成员,分别 表示字符串的长度和字符串的内容,有一个构造函数和一个析 构函数,函数GetLength( )返回字符串长度,函数 GetContents( )获得字符串的内容,重载函数SetContents( )给 字符串设置值。 #include <iostream.h> #include <string.h> class CString { private: int length; char *contents; public: CString(); //构造函数 ~CString(); //析构函数 int GetLength(); void GetContents(char *str); void SetContents(int len, char *cont); void SetContents(char *cont); }; 第6章 类与对象
第6章类与对象 例6.2(续一) CString: CString length =0; contents E null cout<<"字符串对象初始化”<end; CString: -CString cout<< contents<"'被销毁"<<endl; if(contents NULL) delete contents. int CString: GetLengtho return length; void CString: GetContents(char *str) strcpy(str,contents);
例6.2 (续一) CString::CString() { length = 0; contents = NULL; cout << "字符串对象初始化" << endl; } CString::~CString() { cout << contents << "被销毁" << endl; if(contents != NULL) delete contents; } int CString::GetLength() { return length; } void CString::GetContents(char *str) { strcpy(str, contents); } 第6章 类与对象
第6章类与对象 例6.2(续二) void CString: Setcontents(int len, char *cont length len; if(contents NULL delete contents 重载函数 SetContents() contents new char[len+1] 都是将要设置的字符串 strcpy(contents, cont); 长度赋给数据成员 length, cout<"两个参数的 SetContents函数<endl;然后判断原来数据成员 contents是否已经有数 void CString: Setcontents( char *cont) 据(即已经不为空NULL ),如果已不为空 length strlen(cont); 则先释放原来的内存 if(contents E NULL) 再根据新字符串的长度 重新申请内存。 delete contents; contents new char[length+1l; strcpy(contents, cont); cout<<"一个参数的 SetContents函数<endl;
例6.2 (续二) void CString::SetContents(int len, char *cont) { length = len; if(contents != NULL) delete contents; contents = new char[len+1]; strcpy(contents,cont); cout << "两个参数的SetContents函数" << endl; } void CString::SetContents( char *cont) { length = strlen(cont); if(contents != NULL) delete contents; contents = new char[length+1]; strcpy(contents,cont); cout << "一个参数的SetContents函数" << endl; } 第6章 类与对象 重载函数SetContents( ) 都是将要设置的字符串 长度赋给数据成员length, 然后判断原来数据成员 contents是否已经有数 据(即已经不为空NULL 了),如果已不为空, 则先释放原来的内存, 再根据新字符串的长度 重新申请内存
第6章类与对象 例6.2(续三) void maino CString str1,str2;两次调用构造函数 str1. SetContents("第一个字符申");∥调用有一个参数的 Setcontents函数 str2 SetContents(20,"第二个字符串两个参数);∥调用有两个参数的 Setcontent函数 int i=str1. Getlengtho char string[ 100]: str1.Getcontents(string) cout <<i<<< string < end: i=str2. GetLengtho 程序运行结果为: str2. GetContents(string) 字符串对象初始化 cout≤≤i<<""≤< string≤<endl; 字符串对象初始化 个参数的 SetContents函数 两个参数的 SetContents函数 12第一个字符串 20第二个字符串两个参数 第二个字符串两个参数被销毁 第一个字符串被销毁
例6.2 (续三) void main() { CString str1,str2; //两次调用构造函数 str1.SetContents("第一个字符串"); //调用有一个参数的SetContents函数 str2.SetContents(20, "第二个字符串两个参数"); //调用有两个参数的SetContents函数 int i = str1.GetLength(); char string[100]; str1.GetContents(string); cout << i << " "<< string << endl; i = str2.GetLength(); str2.GetContents(string); cout << i << " " << string << endl; } 第6章 类与对象 程序运行结果为: 字符串对象初始化 字符串对象初始化 一个参数的SetContents函数 两个参数的SetContents函数 12 第一个字符串 20 第二个字符串两个参数 第二个字符串两个参数被销毁 第一个字符串被销毁