C++ Exercises(六)
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;
}
{
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;
}
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2007-07-04 15:40 Phinecos(洞庭散人) 阅读(207) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述