这是一个非常经典的面试题,能够测试学生很短的时间C++法师是综合,答案应包含C++大多数知识类,保证书String符类可以完成值、抄、的变量和其它函数的定义。

#include<iostream>
using namespace std; class String
{
public:
String(const char *str=NULL);
String(const String &other);
~String(void);
String &operator =(const String &other);
private:
char *m_data;
}; String::String(const char *str)
{
cout<<"构造函数被调用了"<<endl;
if(str==NULL)//避免出现野指针,如String b;假设没有这句话。就会出现野
//指针
{
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(void)
{
delete m_data;
cout<<"析构函数被调用了"<<endl;
} String::String(const String &other)
{
cout<<"赋值构造函被调用了"<<endl;
int length=strlen(other.m_data);
m_data=new char[length+1];
strcpy(m_data,other.m_data);
}
String &String::operator=(const String &other)
{
cout<<"赋值函数被调用了"<<endl;
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;
}
void main()
{
String b;//调用构造函数
String a("Hello");//调用构造函数
String c("World");//调用构造函数
String d=a;//调用赋值构造函数。由于是在d对象建立的过程中用a来初始化
d=c;//重载的赋值函数调用后,
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

C++达到String分类的更多相关文章

  1. 周赛-Equidistant String 分类: 比赛 2015-08-08 15:44 6人阅读 评论(0) 收藏

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  2. java String分类trim,substring,replaceAll,indexOf使用功能

    1.trim性能 特征去掉字符串首尾空格,防止不必要的空格导致错误. public class TrimTest { public static void main(String[] args) { ...

  3. java 中间String分类

    String a = "aaa"; 用这样的方式的时候java首先在内存中寻找"aaa"字符串.假设有.就把aaa的地址给它 假设没有则创建 String a ...

  4. hdu 1033 (bit masking, utilization of switch, '\0' as end of c string) 分类: hdoj 2015-06-15 21:47 37人阅读 评论(0) 收藏

    bit masking is very common on the lower level code. #include <cstdio> #include <algorithm&g ...

  5. CodeForces - 1221E Game With String 分类讨论

    首先分析A能获胜的情况 A能获胜 当且仅当A拿完后所有剩下的都<b 所以一旦存在一个大小为X的 且 b<=X<a 则必是后手赢 当X为 a<=x<2*b 的时候 无论A或 ...

  6. 20170622xlVBA多部门分类汇总同类合并单元格

    Public Sub Basic_CodeFrame() AppSettings On Error GoTo ErrHandler Dim StartTime, UsedTime As Variant ...

  7. PIE SDK 精度分析(分类后处理)

    1.算法功能简介 遥感图像分类精度分析通常把分类图与标准数据进行比较,然后用正确分类的百分比来表示分类的精度. PIE SDK支持算法功能的执行,下面对精度分析算法功能进行介绍. 2.算法功能实现说明 ...

  8. 通用访问 - 用“反射”来设计通用的通信协议,以及配套的SDK、工具

    1. 效果演示 2. 通信协议 功能介绍 特点 TCP协议 WebApi协议 3. SDK与工具 4. 应用示例 迷你网管 通用GIS 系统管理 5. 设计初衷与演化   1. 效果演示     服务 ...

  9. BizTalk开发系列(二十二) 开发自定义Map Functoid

    尽管 BizTalk Server 提供许多Functoid以支持一系列不同的操作,但仍可能会遇到需要其他方法的情况.<BizTalk开发系列 Map扩展开发>介绍了通过使用自定义 XSL ...

随机推荐

  1. 基于ICSharpCode.SharpZipLib.Zip的压缩解压缩

    原文:基于ICSharpCode.SharpZipLib.Zip的压缩解压缩 今天记压缩解压缩的使用,是基于开源项目ICSharpCode.SharpZipLib.Zip的使用. 一.压缩: /// ...

  2. “ddl”有一个无效 SelectedValue,因为它不在项目列表中。

    “ddl_ekt”有一个无效 SelectedValue,因为它不在项目列表中. 怎么回事 现象: 在用户控件的page_load事件里绑定下拉框,报上面错误 解决: 将下拉框绑定,放在page_In ...

  3. CareerCup它1.8 串移包括问题

    [称号] 原文: 1.8 Assume you have a method isSubstring which checks if one word is a substring of another ...

  4. JAVA开源爬虫,WebCollector,使用方便,有接口。

    假设你想下载整个网站内容爬行动物,我不希望配置heritrix复杂的爬行动物,要选择WebCollector.项目github一个不断更新. github源地址:https://github.com/ ...

  5. UML学习(一)类图和对象图

    对象是一个概念,一种抽象或者事物.对象能够是具有现实意义的事物,也能够是抽象的一个概念.比方,一家公司或者一个进程. 类是一组对象的集合或者抽象的概念.类具有同样的属性和方法. 介绍完基本对象和类的基 ...

  6. hdu 4661 Message Passing(木DP&amp;组合数学)

    Message Passing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  7. Linux/UNIX数据文件和信息系统

    数据文件和信息系统 密码文件 在存储/etc/passwd在.以下功能可以用来获得密码文件条目. #include <sys/types.h> #include <pwd.h> ...

  8. 可以改变文本行距(行间距)的Label

    ////////////////////////////////////////////////////// /// ///功能:可以改变文本行距(行间距)的Label ///作者:emanlee / ...

  9. Windows 驱动发展基金会(九)内核函数

    Windows 驱动发展基金会系列,转载请注明出处:http://blog.csdn.net/ikerpeng/article/details/38849861 这里主要介绍3类Windows的内核函 ...

  10. HDOJ 5000 Clone

    所有的属性,以满足一定的条件,是,财产和等于sum/2结果最大. Clone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...