字符串类改进版(加入了运算符重载)
来源:互联网 发布:java体系结构 编辑:程序博客网 时间:2024/05/29 19:29
#include<iostream>
#include<string>
using namespace std;
class Mystring
{
private:
char *m_buf;
public:
Mystring(char *p=""):m_buf(p)
{
this->m_buf=new char[strlen(p)+1];
strcpy(m_buf,p);
}
Mystring(const Mystring &s)
{
m_buf=new char[strlen(s.m_buf)+1];
strcpy(m_buf,s.m_buf);
}
~Mystring()
{
delete []m_buf;
}
void show()const;
int Getlength()const;
Mystring & operator=(const Mystring &s);
Mystring operator+(const Mystring &s);
//ostream & operator<<(ostream &c);
char operator [](int i);
};
char Mystring::operator [](int i)
{
return m_buf[i];
}
void Mystring::show()const
{
cout<<this->m_buf<<endl;
}
int Mystring::Getlength()const
{
return strlen(this->m_buf);
}
/*ostream & Mystring:: operator<<(ostream &c)
{
c<<this->m_buf;
return c;
}*/
Mystring & Mystring::operator=(const Mystring &s)
{
delete []m_buf;
m_buf=new char[s.Getlength()+1];
strcpy(m_buf,s.m_buf);
return *this;
}
Mystring Mystring::operator+(const Mystring &s)
{
//delete []m_buf;
Mystring r;
delete []r.m_buf;
r.m_buf=new char[s.Getlength()+this->Getlength()+1];
strcpy(r.m_buf,this->m_buf);
strcat(r.m_buf,s.m_buf);
return r;
}
void main()
{
Mystring m("hello");
Mystring k("abc");
Mystring f=m+k;
//m.Getlength();
//cout<<m.Getlength()<<endl;//测试用途
//m.Copy(k);
f.show();
m=k;
m.show();
int i;
for(i=0;i<f.Getlength();i++)
{
cout<<f[i]<<endl;
}
}
- 字符串类改进版(加入了运算符重载)
- 十九、运算符重载(三)String类的改进实现、[]运算符重载、+运算符重载、+=运算符重载、<<运算符重载、>>运算符重载
- 自定义的精简版String类,重载了各种字符串的运算符操作
- 重载运算符了
- 第十三周项目1-分数类中的运算符重载(改进)
- 字符串类中重载运算符
- 字符串类_运算符重载
- 二十、运算符重载(四) Integer类改进、类型转换运算符重载、 ->运算符重载、 new的三种用法、operator new、operator delete重载
- 运算符重载和字符串
- 字符串 赋值运算符 重载
- C++ 字符串重载运算符
- 字符串的运算符重载
- 字符串的运算符重载
- 第八周【项目3-分数类中的运算符重载】改进
- [c++]String字符串类的运算符重载
- 字符串类中运算符重载出现的一个问题
- Java 深究字符串String类(1)之运算符"+"重载
- 类运算符重载
- 英文网站 CSS 字体写哪种?
- java 文件下载且解决中文乱码
- 对python的比较评价
- 5种流行的Linux发行版:你更喜欢哪一个呢?
- 计算机编码相关
- 字符串类改进版(加入了运算符重载)
- 小白学习freemark的过程(代码全贴+详细介绍)
- oracle数据类型
- hostname
- 微软提供的一些实用小工具集
- zf异常处理机制
- linux 内核与用户空间通信之netlink使用方法
- 游戏共享模式的参考
- 鸡尾酒排序Linux下c 实现