封装字符串类
来源:互联网 发布:linux开机启动root脚本 编辑:程序博客网 时间:2024/05/14 13:06
封装CString字符串类,并实现如下功能:
1.实现含参构造函数,将字符串作为参数,可以构造一个包含该字符串的字符串对象。
2.实现重载拷贝构造函数,实现深拷贝。
3.实现无参构造函数。
4.重载 [ ] 符号,实现返回字符串对象中指定位置字符的功能。如n[4],返回对象n字符串的第4个字符。
5.实现成员函数,调用时返回对象代表的字符串的长度。
6.重载 ( ) 符号,实现截取字符串的功能,如m(2,8),返回m对象代表的字符串中的第2位到第8位。
#include<iostream>using namespace std;class CString {private: char* str; int length;public: CString():str(NULL),length(0){}//无参构造函数 CString(const char*input);//含参构造函数 CString(const CString&temp);//拷贝构造函数 ~CString() { delete[]str; length = 0; }//析构函数 int len() { return length; }//返回长度函数 char* getString() { return str; }//返回字符串函数 char operator[](int);//取特定位字符 CString operator()(int, int);//截取字符串 friend CString operator+(CString& a, CString& b);//连接字符串 CString& operator=(CString& temp);//重载等于号};using namespace std;CString::CString(const char*input)//字符串构造函数{ int count = 0, i; while (input[count] != '\0')//结束后,count在数值上等于字符串的长度 { count++; } str = new char[count + 1];//申请字符串长度加1的内存空间 for (i = 0; i<count; i++) { str[i] = input[i];//依次对字符赋值 } str[i] = '\0'; length = count;}CString::CString(const CString& temp)//拷贝构造函数{ int i; length = temp.length; str = new char[temp.length + 1];//多申请一位,最后一位放'\0' for (i = 0; i < temp.length; i++) { str[i] = temp.str[i]; } str[i] = '\0';}char CString::operator[](int temp)//返回字符值{ if (temp <= length + 1 && temp >= 0) { return str[temp - 1]; } else { cout << "取值超出范围!" << endl; return '\0'; }}CString CString::operator()(int l, int r)//截取字符串{ CString temp; if (l > r || r > this->length || l<1) { cout << "输入范围有误!" << endl; return temp; } int i, j; temp.length = r - l + 1; temp.str = new char[temp.length+1]; for (i = l - 1, j = 0; i < r; i++, j++) { temp.str[j] = this->str[i]; } temp.str[j] = '\0'; return temp;}CString operator+(CString& a, CString& b)//字符串连接{ int i,j; CString temp; temp.length = a.length + b.length; temp.str = new char[temp.length + 1]; for (i = 0; i<a.length; i++) { temp.str[i] = a.str[i]; } for (i = a.length, j = 0; j<b.length; i++, j++) { temp.str[i] = b.str[j]; } temp.str[a.length + b.length] = '\0'; return temp;}CString& CString::operator=(CString& temp) { int i; this->length = temp.length; delete[]this->str; this->str = new char[temp.length + 1]; for (i = 0; i < temp.length; i++) { this->str[i] = temp.str[i]; } this->str[i] = '\0'; return *this;} int main(){ CString m("software");//含参构造函数 CString n(m);//拷贝构造函数 CString o;//无参构造函数 //调用getString()函数显示结果 cout << "含参构造函数结果:" << m.getString() << endl; cout << "拷贝构造函数结果:" << n.getString() << endl; o = m;//使用拷贝构造函数 cout << "使用等号调用拷贝构造函数结果:" << o.getString() << endl; cout << "重载中括号结果:" << n[4] << endl;//重载中括号 cout << "返回n的字符串长度:" << n.len() << endl;//成员函数返回字符串长度 o = m + n;//重载+号,连接字符串 cout << "连接字符串结果:" << o.getString() << endl; o = m(2, 8);//重载(),截取字符串 cout << "截取字符串结果:" << o.getString() << endl; system("pause"); return 0;}
0 0
- C++字符串封装类
- 字符串类的封装
- 封装字符串类
- 字符串封装类(非原创)
- 收藏.C++.字符串封装类
- 常用类封装--随机字符串
- 字符串封装
- 封装 字符串
- 字符串加密解密类[封装][C#]
- 正则表达式实现替换字符串封装类
- 类的封装(消息和字符串)
- C++字符串完全指引 —— 字符串封装类
- C++字符串完全指引之二 - 字符串封装类
- 分隔字符串的封装
- Java 字符串操作封装
- 封装字符串转码
- 字符串的封装
- Java 字符串操作封装
- 算法笔记——【贪心算法】单源最短路径问题
- 外排序多路归并+败者树-算法学习笔记十五
- 一个组合数证明
- Jsp与Servlet面试题
- javaweb--struts+Hibernate把表单数据存储到数据库
- 封装字符串类
- 蓝牙HCI剖析(一)
- Problem B: 来开个书店吧
- 关于认可
- 欢迎使用CSDN-markdown编辑器
- 面试题之JavaScript+Jquery
- Spring Security4.0.3源码分析之FilterChainProxy初始化
- 4.2.2 查看表结构 SHOW COLUMNS 或DESCRIBE
- 实现银行账户信息管理