C++ 类,运算符重载--->代码

本文详细介绍了C++中自定义字符串类的实现方法,包括构造函数、复制构造函数、析构函数、赋值运算符、长度获取、空字符串判断、流插入运算符和字符串连接运算符的实现。并通过实例展示了如何使用该类进行字符串操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个不错的学习文档:
     http://www.kuaipan.com.cn/file/id_204066781134907.htm
     在线查看:

/*mystring.h*/
#include <iostream>
using namespace std;

class MyString
{
friend ostream &operator<<(ostream &,const MyString &);
friend MyString operator+(const MyString&,const MyString &);

public:
	MyString(const char *);
	MyString(const MyString &);
	~MyString(void);
	MyString&operator=(const MyString &);
	int length(void)const;
	bool isEmpty(void)const;
private:
	char *data;
	int len;
};


/*mystring.cpp*/
#include <iostream>
#include "mystring.h"
#include <cstring>
using namespace std;

MyString::MyString(const char *str)
{
cout<<"ctor"<<endl;
if(str==NULL)
{
len=0;
data=new char[1];
*data='\0';
}else{
len=strlen(str);
data=new char[len+1];
strcpy(data,str);
}

}
MyString::MyString(const MyString &other)
{
cout<<"copy ctor"<<endl;

len=other.len;
data=new char [len+1];
strcpy(data,other.data);

}
MyString::~MyString(void)
{
cout<<"assignment"<<endl;
delete[]data;
}
MyString &MyString::operator=(const MyString &other)
{
cout<<"assignment"<<endl;
if(this==&other) //注意自己
return *this;
delete[]data;//注意以前的长度不狗

len=other.len;
data=new char[len+1];
strcpy(data,other.data);
return *this;
}
int MyString::length(void )const
{
return len;
}
bool MyString::isEmpty(void )const
{
return len==0;
}
ostream& operator<<(ostream &out,const MyString &str)
{
out<<str.data;
return out;
}
MyString operator+(const MyString &a,const MyString &b)
{
cout<<"concatenate"<<endl;
MyString temp("");

delete[]temp.data;
temp.len=a.len+b.len;
temp.data=new char[temp.len+1];
strcpy(temp.data,a.data);
strcat(temp.data,b.data);

return temp;
}

/*main.cpp*/
#include <iostream>
#include "mystring.h"

using namespace std;

int main()
{
	cout<<"----------NO.1--start----------"<<endl;
	MyString str("Hello");  //NO.1
	cout<<"----------NO.1--END----------"<<endl;

	cout<<"----------NO.2--start----------"<<endl;
	MyString str1=str;     //NO.2
	cout<<"----------NO.2--END----------"<<endl;
	cout<<"Length of str1:"<<str1.length()<<endl;
	cout<<"Value of str1:"<<str<<endl;

	cout<<"----------NO.3--start----------"<<endl;
	MyString str2="";//NO.3
	cout<<"----------NO.3--END----------"<<endl;
	cout<<"ls str2 empty()?"<<(str2.isEmpty()?"ture":"false")<<endl;
	
	cout<<"----------NO.4--start----------"<<endl;
	str2="world"; //NO.4
	cout<<"----------NO.4--END----------"<<endl;
	cout<<"Now value of str2"<<str2<<endl;

	cout<<"Now value of str:"<<str1+str2<<endl;
	cout<<"Value of another expression:"<<str1+"__world"<<endl;
	cout<<"Value of yet another expression:"<<"hello,"+str2<<endl;

	cout<<"----------NO.5--start----------"<<endl;
	MyString str3=str1+str2; //NO.5
	cout<<"----------NO.5--END----------"<<endl;

	cout<<"Value of str3:"<<str3<<endl;

	return 0;

}









                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值