编写String 类的构造函数,析构函数和赋值函数
来源:互联网 发布:偏头痛物理疗法知乎 编辑:程序博客网 时间:2024/06/06 16:27
//.......................编写String 类的构造函数,析构函数和赋值函数#if 0#include<iostream>#include<string>using namespace std;class String { friend ostream & operator<<(ostream& o,const String &str); 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 length = strlen(str);m_data = new char[length+1];strcpy(m_data,str);} } String::String (const String &other) {int length = strlen(other.m_data );m_data = new char[length+1];strcpy(m_data,other.m_data ); } String::~String () {delete []m_data; } String &String:: operator = (const String &other) {if(this == &other) //检查自赋值return *this;delete []m_data; //释放原有的内存资源int length = strlen(other.m_data);m_data = new char[length+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; }#endif
阅读全文
0 0
- 编写类String的构造函数、拷贝构造函数、析构函数和赋值函数
- 编写类String 的构造函数、拷贝构造函数、析构函数和赋值函数
- 网易游戏笔试题:编写string类的构造函数、拷贝构造函数、赋值构造函数和析构函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数,析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String 的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 如何正确编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String 的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 原来编程还可以这样学习
- Mysql单表操作
- bzoj1577 [Usaco2009 Feb]庙会捷运Fair Shuttle 贪心+线段树
- AndroidStudio之jcenter mavenCentral
- linux shell命令的快捷键
- 编写String 类的构造函数,析构函数和赋值函数
- Pycharm环境下使用第三方包
- 欢迎使用CSDN-markdown编辑器
- 图像裁剪框架uCrop-自我使用发现(1)
- Linux命令之常用用户管理命令
- Windows安装MySQL
- N!C语言实现
- JZOJ2134.2017.05.20【usaco2017_Mar Bronze & Silver】C组T2Bovine Genomics
- 反射之动态代理的概述和实现