类String的 构造函数、拷贝构造函数、析构函数、赋值函数实现
2010-11-02 15:55
387 查看
#include<iostream> #include<stdio.h> #include<string> #include<string.h> #include<algorithm> #include<time.h> #include<cmath> #include<iomanip> #include<stdlib.h> #include<queue> #include<vector> using namespace std; class String { public: String(const char * str=NULL); String(const String& other); ~String(); String& operator =(const String& other); char * m_data; }; String::String(const char *str ) { if(str==NULL){ m_data=new char[1]; m_data[0]='/0'; } else{ int len=strlen(str); m_data=new char[len+1]; strcpy(m_data,str); } } 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() { delete[] 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; } int main() { String s1("Jason"); cout<<s1.m_data<<endl; String s2=s1; cout<<s2.m_data<<endl; String s3(s1); cout<<s3.m_data<<endl; String s4; s4=s1; cout<<s4.m_data<<endl; system("pause"); return 0; }
相关文章推荐
- 字符串strcpy函数的实现,以及字符串string的实现,编写构造函数,拷贝构造函数,赋值函数,析构函数
- 类String的构造函数、拷贝构造函数、析构函数、赋值函数
- String构造函数 拷贝构造函数 析构函数 赋值构造函数的实现
- 面试题目6 编写类String的构造函数、拷贝构造函数、析构函数、赋值函数
- 编写类String的构造函数、拷贝构造函数、析构函数、赋值函数
- C++实现一个类并包含构造函数,拷贝构造函数,赋值函数,析构函数
- 自主编程实现String类的构造函数,析构函数,拷贝构造函数,赋值函数
- String的构造函数,拷贝构造函数、析构函数和赋值函数
- 实现MyString类--构造函数、拷贝构造函数、析构函数、赋值函数、操作符重载函数
- 详解C++ 编写String 的构造函数、拷贝构造函数、析构函数和赋值函数
- 编写类String的构造函数、拷贝构造函数、析构函数和赋值函数
- 编写类String的构造函数、拷贝构造函数、析构函数和赋值函数
- 实现string的构造函数,析构函数,拷贝构造函数
- String 的普通构造函数、拷贝构造函数、析构函数、赋值函数
- 编写String的构造函数、析构函数、拷贝构造函数、赋值函数
- 实现C++类中默认的四个方法默认构造函数、析构函数、拷贝构造函数、赋值函数
- String 构造函数,析构函数,拷贝构造函数和赋值函数
- 编写类String 的构造函数、拷贝构造函数、析构函数和赋值函数
- 类String的构造函数、析构函数、拷贝构造函数和赋值函数
- 类String的构造函数、析构函数和赋值函数