class String  
{
public:
String();
String(const String& another);//拷贝构造函数
explicit String(const char* st);
explicit String(int size);
String& operator =(const String& another);//赋值构造函数
virtual ~String();
int GetLength()const;
void assign(const char* st);
void print()const;
bool operator ==(const String& another)const;//是否相等
bool operator !=(const String& another)const; 
friend String& operator +(const String& first,const String& second);
private:
char *value;
int len;
};
#include "stdafx.h"
#include "String.h"
#include <string.h>
#include <iostream.h>
//
// Construction/Destruction
//
String::String()
{
this->len = 0;
this->value = NULL;
}
String::String(const String& another)
{
int len1 = another.GetLength();
this->value = new char[len1+1];
strcpy(this->value,another.value);
this->len = len1;
}
String::String(const char* st)
{
int len1 = strlen(st);
this->value = new char[len1+1];
strcpy(this->value,st);
this->len = len1;
}
String::String(int size)
{
this->value = new char[size+1];
this->len = size;
}
String& String::operator =(const String& another)
{
int len1 = another.GetLength();
if(this->value!=NULL)
{
delete []this->value;
}
this->value = new char[len1+1];
strcpy(this->value,another.value);
this->len = len1;
return *this;
}
String::~String()
{
delete [] this->value;
}
int String::GetLength()const
{
return this->len;
}
void String::assign(const char *st)
{
int len1 = strlen(st);
if(this->value!=NULL)
{
delete []this->value;
}
this->value = new char[len1+1];
strcpy(this->value,st);
this->len = len1;
}
void String::print()const
{
cout<<this->value<<"\nLength: "<<this->len<<endl;
}
bool String::operator ==(const String& another)const
{
if(strcmp(this->value,another.value)==0)
{
return true;
}
else
{
return false;
}
}
bool String::operator !=(const String& another)const
{
return !(*this==another);
}
String& operator +(const String& first,const String& second)
{
String *tmp = new String(first.len+second.len);
strcpy(tmp->value,first.value);
strcat(tmp->value,second.value);
return *tmp;
}
// demo1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "String.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
String str1,str3;
char name[10]="dyk";
str1.assign("hello,world");
str1.print();
String str2(str1);
str2.print();
str3 = str2;
str3.print();
if(str1==str2)
{
cout<<"str1==str2!!!"<<endl;
}
str3.assign(name);
str3.print();
String* pStr4 = new String(str3);
pStr4->print();
String str5(name);
//    str5 = "phinecos";
//当类定义中提供了单个参数的构造函数时,
//该类便提供了一种将其他数据类型的数值或变量转换为用户所定义数据类型
str5.print();
str5=str1+str2;
str5.print();
return 0;
}
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/07/04/805782.html,如需转载请自行联系原作者

C++ Exercises(六)相关推荐

  1. C++ Exercises(十六)---Ethernet帧包结构解析

    图1是一个假想的帧包结构, 图2是解包后的结果. ///// ///帧信息类  ///// class CFrame { public:     CFrame(void);     ~CFrame(v ...

  2. Docker入门六部曲——Swarm

    原文链接:http://www.dubby.cn/detail.html?id=8738 准备工作 安装Docker(版本最低1.13). 安装好Docker Compose,上一篇文章介绍过的. 安 ...

  3. Docker入门六部曲——Stack

    原文链接:http://www.dubby.cn/detail.html?id=8739 准备知识 安装Docker(版本最低1.13). 阅读完Docker入门六部曲--Swarm,并且完成其中介绍 ...

  4. Docker入门六部曲——服务

    原文链接:http://www.dubby.cn/detail.html?id=8735 准备 已经安装好Docker 1.13或者以上的版本. 安装好Docker Compose.如果你是用的是Do ...

  5. PyTorch 学习笔记(六):PyTorch hook 和关于 PyTorch backward 过程的理解 call

    您的位置 首页 PyTorch 学习笔记系列 PyTorch 学习笔记(六):PyTorch hook 和关于 PyTorch backward 过程的理解 发布: 2017年8月4日 7,195阅读 ...

  6. 受用一生的高效 PyCharm 使用技巧(六)

    http://www.sohu.com/a/329854019_654419 大家好,今天我又来给大家更新 PyCharm 的使用技巧. 从第一篇开始,一直到本篇,一共更新了6篇文章,每篇 5 个小技 ...

  7. 操作系统学习笔记 第六章:设备管理(王道考研)

    本文章基于网课: 2019 王道考研 操作系统 考试复习推荐资料:操作系统复习总结 - 百度文库 (baidu.com) 需要相关电子书的可以关注我的公众号BaretH后台回复操作系统 第一章:操作系 ...

  8. 王道考研 计算机网络笔记 第六章:应用层

    本文基于2019 王道考研 计算机网络: 2019 王道考研 计算机网络 个人笔记总结 第一章:王道考研 计算机网络笔记 第一章:概述&计算机网络体系结构 第二章:王道考研 计算机网络笔记 第 ...

  9. TVM性能评估分析(六)

    TVM性能评估分析(六) Figure 1. The workflow of development PC, compile, deploy to the device, test, then mod ...

最新文章

  1. c语言错误2015,C语言2015(回答).doc
  2. swift 实践- 10 -- UIProgressView
  3. python不定长参数怎么相加_python函数不定长参数使用方法解析
  4. 《剑指offer》--二维数组中的查找、从头到尾打印链表、重建二叉树、旋转数组的最小数字
  5. JavaScript中数组去重的几种方法整理
  6. hibernate DetachedCriteria实现多表关联查询createAlias的使用
  7. java微信web支付开发_java实现微信H5支付方法详解
  8. 小程序云开发表单提交并在页面中获取数据
  9. 实训报告html前端开发,web前端开发实习报告比你想象中简单
  10. PHP设置表格框线,ppt中表格边框线条怎样设置?
  11. 项目管理模式:项目型、职能型、矩阵型
  12. html鼠标指向上面有尾注,插入脚注和尾注的方法
  13. 用python计算工资工资_php项目中用python来预测薪资(工资)
  14. 企业微信应用授权/静默登录
  15. Java实现邮箱登录验证和邮件发送
  16. 父进程退出,保证子进程交由init。
  17. 思科开源软交换机vpp环境搭建
  18. 数电实验(一)利用与非门设计四舍五入判别电路
  19. 怎样用计算机画图抠图,画图抠图透明,画图工具怎么改底色
  20. Windows远程桌面连接与内网穿透详细步骤

热门文章

  1. HDU 1824 Let's go home
  2. jquery easyui 动态绑定数据列
  3. axure rp pro 6.5
  4. 构建 编译和运行Urho3D工程
  5. 【WPF】动态设置Binding的ConverterParameter转换器参数
  6. hibernate---java.lang.UnsupportedOperationException: The user must supply a JDBC connection
  7. 正则表达式懒惰贪婪和replace函数
  8. jpa多条件查询重写Specification的toPredicate方法(转)
  9. (转)ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果
  10. Android开源测试框架