编写类String 的构造函数、析构函数和赋值函数,已知类String 的原型为:
class String
{
public:
String(const char *str = NULL); // 普通构造函数
String(const String &other); // 拷贝构造函数
~ String(void); // 析构函数
String & operate =(const String &other); // 赋值函数
private:
char *m_data; // 用于保存字符串
};

#include <iostream>
class String
{
public:
String(const char *str=NULL);//普通构造函数
String(const String &str);//拷贝构造函数
String & operator =(const String &str);//赋值函数
~String();//析构函数
protected:
private:
char* m_data;//用于保存字符串
}; //普通构造函数
String::String(const char *str)
{
if (str==NULL)
{
m_data=new char[]; //对空字符串自动申请存放结束标志'\0'的空间
if (m_data==NULL)
{//内存是否申请成功
std::cout<<"申请内存失败!"<<std::endl;
exit();
}
m_data[]='\0';
}
else
{
int length=strlen(str);
m_data=new char[length+];
if (m_data==NULL)
{//内存是否申请成功
std::cout<<"申请内存失败!"<<std::endl;
exit();
}
strcpy(m_data,str);
}
} //拷贝构造函数
String::String(const String &str)
{ //输入参数为const型
int length=strlen(str.m_data);
m_data=new char[length+];
if (m_data==NULL)
{//内存是否申请成功
std::cout<<"申请内存失败!"<<std::endl;
exit();
}
strcpy(m_data,str.m_data);
}
//赋值函数
String& String::operator =(const String &str)
{//输入参数为const型
if (this==&str) //检查自赋值
return *this;
int length=strlen(str.m_data);
delete [] m_data;//释放原来的内存资源
m_data= new char[length+];
if (m_data==NULL)
{//内存是否申请成功
std::cout<<"申请内存失败!"<<std::endl;
exit();
}
strcpy(m_data,str.m_data);
return *this;//返回本对象的引用
} //析构函数
String::~String()
{
delete [] m_data;
} void main(){
String a;
String b("abc");
system("pause");
}

原始文章:http://blog.csdn.net/zhuimengzh/article/details/6708882

编写类String 的构造函数、析构函数和赋值函数的更多相关文章

  1. C/C++面试题:编写类String的构造函数、析构函数和赋值函数。

    转https://www.cnblogs.com/alinh/p/9636500.html 考点:构造函数.析构函数和赋值函数的编写方法出现频率:☆☆☆☆☆已知类String的原型为:         ...

  2. 编写类String的构造函数、析构函数和赋值函数

    已知类String的原型为: class String {   public:  String(const char *str = NULL); // 普通构造函数  String(const Str ...

  3. 编写类String的构造函数、拷贝构造函数、析构函数和赋值函数

    一.题目: class String { public: String(const char *str = NULL); // 普通构造函数 String(const String &othe ...

  4. String的构造函数、析构函数和赋值函数

    编写类String的构造函数.析构函数和赋值函数 已知类String的原型为: class String { public: String(const char *str = NULL); // 普通 ...

  5. 类string的构造函数、拷贝构造函数和析构函数

    原文:http://www.cnblogs.com/Laokong-ServiceStation/archive/2011/04/19/2020402.html   类string的构造函数.拷贝构造 ...

  6. C++反汇编第一讲,认识构造函数,析构函数,以及成员函数

    C++反汇编第一讲,认识构造函数,析构函数,以及成员函数 以前说过在C系列下的汇编,怎么认识函数.那么现在是C++了,隐含有构造和析构函数 一丶认识构造函数 高级代码: class MyTest { ...

  7. C++(1)C++类四个默认函数---构造函数、析构函数、拷贝函数、赋值函数

    C++构造函数和析构函数 默认构造函数指不带参数或者所有参数都有缺省值的构造函数!!! (1)构造函数.析构函数与赋值函数 构造函数.析构函数与赋值函数是每个类最基本的函数.它们太普通以致让人容易麻痹 ...

  8. CPP_类默认函数:构造函数,拷贝构造函数,赋值函数和析构函数

    类默认函数:构造函数,拷贝构造函数,赋值函数和析构函数 // person.h #ifndef _PERSON_H_ #define _PERSON_H_ class Person{ public : ...

  9. C++ 拷贝构造函数与赋值函数的区别(很严谨和全面)

    这里我们用类String 来介绍这两个函数: 拷贝构造函数是一种特殊构造函数,具有单个形参,该形参(常用const修饰)是对该类类型的引用.当定义一个新对象并用一个同类型的对象对它进行初始化时,将显式 ...

随机推荐

  1. Installshield在安装结束时刷新系统

    原文:Installshield在安装结束时刷新系统 在OnEnd里添加代码,两种解决方案 群友kevin的解决方案 #include "ifx.h"  //Call to Win ...

  2. ASP.NET中 RegularExpressValidator(正则验证)的使用

    原文:ASP.NET中 RegularExpressValidator(正则验证)的使用 ylbtech-ASP.NET-Control-Validator: RegularExpressValida ...

  3. thinkphp 支付宝错误 Class 'Think' not found

    Class 'Think' not found D:\www\DonatePlatform\ThinkPHP\Extend\Vendor\alipay\lib\alipay_submit.class. ...

  4. 在Eclipse发展Webapp部署过程,缓存的位置

    介绍: 在Eclipse进行Web发展,通常直接在项目Eclipse集成Tomcat发展.那Webapp部署在那里?是否在高速缓冲存储器的位置,可以切换? 1.  查看当前的Webapp项目缓存位置 ...

  5. 从零开始学习jQuery(剧场版) 你必须知道的javascript

    原文:从零开始学习jQuery(剧场版) 你必须知道的javascript 一.摘要 本文是jQuery系列教程的剧场版, 即和jQuery这条主线无关, 主要介绍大家平时会忽略的一些javascri ...

  6. jQuery EasyUI API - Layout - Layout[原创汉化官方API]

    最近在学习jQuery EasyUI,发现中文的文档好少,部分文档不错但它是鸟语的,为了大家也为了自己学习吧,汉化做一下笔记. 有没有说清楚的,或者翻译不正确的地方还请大家谅解指出.. 由于工作时间原 ...

  7. JavaScript一个类继承中实现

    JavaScript类是默认原型对象继承: var Person = function() { this.name = "people"; this.hello = functio ...

  8. 安装uBuntu操作系统 - 初学者系列 - 学习者系列文章

    uBuntu是一款不错的Linux操作系统,在上面的应用软件不少,就是说它的支持率挺高.下面就对这款操作系统的安装做下介绍. 1.  下载uBuntu安装文件 打开中文页面.http://www.ub ...

  9. HDU2093--考试排名

    考试排名 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

  10. 【剑指offer】的功率值

    标题叙述性说明: 实现函数double Power(double base, int exponent),求base的exponent次方.不得使用库函数.同一时候不须要考虑大数问题. 分析描写叙述: ...