String类的实现
来源:互联网 发布:eclipse maven mac 编辑:程序博客网 时间:2024/06/07 01:25
#include<iostream>
using namespace std;
class String
{
public:
String(const char *str = NULL);
String(const String &other);
~String(void);
String & operator=(const String &other);
bool operator==(const String &str);
friend ostream & operator<<(ostream& o,const String &str);
private:
char *m_data;
};
/*
构造、析构、拷贝构造、赋值运、流输出运算
*/
String::String(const char *str)
{
if (str == NULL){
m_data = new char[1];
*m_data='\0';
}else{
int len=strlen(str);
m_data = new char[len+1];
strcpy(m_data,str);
}
}
String::~String(void)
{
delete [] m_data;
}
String::String(const String &other)
{
int len = strlen(other.m_data);
m_data = new char[len+1];
strcpy(m_data,other.m_data);
}
String & String::operator=(const String &other)
{
if (this == &other)
return *this;
delete []m_data;
int len = strlen(other.m_data);
m_data = new char[len+1];
strcpy(m_data,other.m_data);
return *this;
}
bool String::operator==(const String &str)
{
return strcmp(m_data,str.m_data) == 0;
}
ostream & operator<<(ostream &o,const String &str)
{
o<<str.m_data;
return o;
int main()
{
String s = "hello";
String s2 = s;
String ss = "hello";
cout<<"s = "<<s<<endl;
cout<<"s2 = "<<s2<<endl;
cout<<boolalpha<<(ss == s)<<endl;
}
using namespace std;
class String
{
public:
String(const char *str = NULL);
String(const String &other);
~String(void);
String & operator=(const String &other);
bool operator==(const String &str);
friend ostream & operator<<(ostream& o,const String &str);
private:
char *m_data;
};
/*
构造、析构、拷贝构造、赋值运、流输出运算
*/
String::String(const char *str)
{
if (str == NULL){
m_data = new char[1];
*m_data='\0';
}else{
int len=strlen(str);
m_data = new char[len+1];
strcpy(m_data,str);
}
}
String::~String(void)
{
delete [] m_data;
}
String::String(const String &other)
{
int len = strlen(other.m_data);
m_data = new char[len+1];
strcpy(m_data,other.m_data);
}
String & String::operator=(const String &other)
{
if (this == &other)
return *this;
delete []m_data;
int len = strlen(other.m_data);
m_data = new char[len+1];
strcpy(m_data,other.m_data);
return *this;
}
bool String::operator==(const String &str)
{
return strcmp(m_data,str.m_data) == 0;
}
ostream & operator<<(ostream &o,const String &str)
{
o<<str.m_data;
return o;
int main()
{
String s = "hello";
String s2 = s;
String ss = "hello";
cout<<"s = "<<s<<endl;
cout<<"s2 = "<<s2<<endl;
cout<<boolalpha<<(ss == s)<<endl;
}
0 0
- string类的实现
- String类的实现
- String类的实现
- String类的实现
- String 类的实现
- String类的实现
- string类的实现
- String类的实现
- String类的实现
- string类的实现
- string类的实现
- string类的实现
- string类的实现
- string类的实现
- string类的实现
- String 类的实现
- String类的实现
- String类的实现
- 最短路径算法1—多源点Floyd
- iOS开发-基本数据类型,对象
- Linux(Centos6.5)下安装svn服务器,并通过http访问
- “对象被多次指定;已忽略多余的指定”并伴有"无法解析的外部符号 "xxx",该符号在函数 "xxx"中被引用的错误"
- Unity3D游戏引擎最详尽基础教程
- String类的实现
- hdoj 5240 Exam
- poj1094Sorting It All Out(拓扑排序)
- iOS开发-类目、延展、协议
- 用递归调用计算和编程计算f(x) 的一些问题处理
- HDU 4777 Rabbit Kingdom [离线+树状数组]
- cocos code ide--js 中获取cocostudio中按钮以及函数回调
- Centos7的相关问题
- smarty学习笔记(01)——小模板