String类的简易实现,用到了构造函数、析构函数、重载运算符、拷贝构造函数、友元函数等知识
2013-09-19 18:44
477 查看
String类的简易实现,用到了构造函数、析构函数、重载运算符、拷贝构造函数、友元函数等知识
参考资料:《高质量C++C编程指南》
运行平台:VS2008
Cpp代码
#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;
}
转自:/article/4244273.html
参考资料:《高质量C++C编程指南》
运行平台:VS2008
Cpp代码
#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;
}
转自:/article/4244273.html
相关文章推荐
- 读书笔记:实现string类的构造函数、拷贝构造函数、析构函数、重载运算符=
- 自主编程实现String类的构造函数,析构函数,拷贝构造函数,赋值函数
- 经典考题——String类的实现(考察构造函数析构函数拷贝构造函数和赋值构造函数)
- string类的实现(构造函数,析构函数,运算符重载)
- 编写string类的构造函数、拷贝构造函数、析构函数、赋值函数
- string类的构造函数、析构函数、拷贝构造函数和赋值函数
- String构造函数 拷贝构造函数 析构函数 赋值构造函数的实现
- 编写一个String类,实现其构造函数、析构函数以及赋值函数
- string类的构造函数,析构函数,拷贝构造函数和赋值构造函数
- C++实现一个类并包含构造函数,拷贝构造函数,赋值函数,析构函数
- C++ 类 & 对象-类成员函数-类访问修饰符-C++ 友元函数-构造函数 & 析构函数-C++ 拷贝构造函数
- string类的构造函数,拷贝构造函数,析构函数和赋值函数
- string类的实现(构造函数,析构函数,运算符重载)
- 网易游戏笔试题:编写string类的构造函数、拷贝构造函数、赋值构造函数和析构函数
- 算法25:实现String类的构造函数,拷贝构造函数和赋值函数
- 编写String类的构造函数、析构函数、拷贝构造函数、赋值函数
- string类构造函数、拷贝构造函数、赋值函数、析构函数
- string类的构造函数,析构函数,拷贝构造函数还有就是赋值函数,然后加上strcpy的函数
- 实现C++类中默认的四个方法默认构造函数、析构函数、拷贝构造函数、赋值函数
- C++面试题,自己实现String类的构造函数,拷贝构造函数,赋值操作符,append,replace,+运算符重载等