文档详情

C++字符串类的构建(一).doc

发布:2017-06-02约1.43千字共3页下载文档
文本预览下载声明
学生姓名: 班级: 学 号: 课程:C++面向对象程序设计 实验题目:字符串类的构建 一 实验地点:实验楼A210 实验目的: 1 C++中类的组成与定义; 2 对象的定义与对象成员的访问; 3 掌握构造函数与析构函数的定义与使用; 4 构造函数的重载; 实验内容: 设计一个字符串类,取名为String,并完成以下工作: 1 定义数据成员; 提示:只需定义一个字符指针成员。 2 定义带默认参数的构造函数(同时完成字符指针类型向类类型的转换); 3 定义拷贝构造函数; 4 定义析构函数; 5 定义字符串拷贝函数——StrCpy; 6 定义字符串输出函数——ToString 7 定义主调函数,并测试上面的函数。 实验程序: #include #include class String public: String s new char[1];s \0; String char *str s new char[strlen str +1]; strcpy s,str ; String const String s0 s new char[strlen s0.s +1]; strcpy s,s0.s ; ~String delete[] s; void ToString cout 输出的字符串为: s endl; String StrCpy char *s1 delete[] s; s new char[strlen s1 +1]; strcpy s,s1 ; return *this; String StrCat String s1 char* p; p new char[strlen s +1]; strcpy p,s ; delete[] s; s new char[strlen s1.s +strlen s +1]; strcpy s,p ; strcat s,s1.s ; return *this; friend void StrCmp String s0,String s1 if strcmp s0.s,s1.s 0 cout s0.s s1.s endl; else if strcmp s0.s,s1.s 0 cout s0.s s1.s endl; else cout s0.s s1.s endl; private: char *s; ; int main String S0 abc ; S0.ToString ; String S1 S0 ,S2 def ,S3 abd ,S4 abc ; S0.ToString ; S0.StrCpy 123 ; S0.ToString ; S0.StrCat S2 ; S0.ToString ; StrCmp S1,S2 ; StrCmp S1,S3 ; StrCmp S1,S4 ; 实验结果: 实验环境(使用的软硬件): 硬件:计算机 软件:Microsoft Visual C++ 6.0 注意: 实验报告以纸质文档形式上交。实验报告将记入平时成绩; 每次实验开始时,交上一次的实验报告,否则将扣除此次实验成绩。 1
显示全部
相似文档