MyString类案例

传智扫地僧课程学习笔记。


数组和字符串类,比较常用,所以要熟悉,

这里一步步实现字符串类,

先创建MyString类的文件,为MyString.h和MyString.cpp,

以及测试案例文件,MyString_Test.cpp,

考虑各种可能的构造情况,

1,MyString s1;

2,MyString s2("s2");

3,MyString s2_2 = NULL;

4,MyString s3 = s2;

对应

1,无参构造函数,MyString();

2,3,有参构造函数,MyString( const char *p);// NULL,作为参数传进去,

4,拷贝构造函数,MyString( const MyString& s);


顺便附上代码,想看的时候,看看,

MyString::MyString()
{
	m_len = 0;
	m_p = new char[m_len+1];
	strcpy( m_p, "");
}
MyString::MyString( const char *p)
{
	if( p == NULL )
	{
		m_len = 0;
		m_p = new char[ m_len+1];
		strcpy( m_p, "");
	}
	else
	{
		m_len = strlen( p);
		m_p = new char[ m_len+1];
		strcpy( m_p, p);
	}
}
MyString::MyString( const MyString& s)
{
	m_len = s.m_len;
	m_p = new char[ m_len +1];
	strcpy( m_p, s.m_p);
}
MyString::~MyString()
{
	if( m_p != NULL )
	{
		delete []m_p;
		m_p = NULL;
		m_len = 0;
	}
}

然后还有一种可能是,

通过等号给对象赋值,

s4 = s2;

s4 =“s2222”;

开始我还以为都是等号,重载一次就行了,

实际上,参数不同,就有不同的重载实现,

前者是对象,后者是字符串,

因此函数类型分别是,

MyString& operator=( const char *p);
MyString& operator=( const MyString &s);

下面是实现的代码,

//s4 = "s2222";
MyString& MyString::operator=( const char *p)
{
	if( m_p != NULL )
	{
		delete [] m_p;
		m_len = 0;
	}
	if( p == NULL )
	{
		m_len = 0;
		m_p = new char[m_len+1];
		strcpy( m_p, "");
	}
	else
	{
		m_len = strlen( p );
		m_p = new char[ m_len+1];
		strcpy( m_p, p);
	}
	return *this;
}

//s4 = s2;
MyString& MyString::operator=( const MyString &s)
{
	if( m_p != NULL )
	{
		delete [] m_p;
		m_len = 0;
	}
	m_len = s.m_len;
	m_p = new char[m_len+1];
	strcpy( m_p, s.m_p);
	return *this;
}
说明一点的是,第二个重载的实现,直接贴的第一个的代码,然后修改了名字,压缩了实现,



再过来是【】的重载,

char& operator[]( int index);

这是实现,

char& MyString::operator[]( int index)
{
	return m_p[index];
}
返回的是字符,所以是字符的引用,


cout<<s4<<endl;

实现代码,

ostream& operator<<( ostream &out, MyString &s)
{
	out<<s.m_p;
	return out;
}
这个是友元函数实现的,所以相较成员函数,多个参数,

cout对应out,s4对应s,



==,也分2种情况,

s2 == s4,即对象和对象的比较,

s2 == “sss”,即对象和字符串的比较,

实现代码如下,

bool MyString::operator==( const char *p )
{
	if( p == NULL )
	{
		if( m_len == 0 )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		if( m_len == strlen( p))
		{
			return !strcmp( m_p, p);
		}
		else
		{
			return false;
		}
	}
	return true;
}
bool MyString::operator==( const MyString &s )
{
	if( m_len != s.m_len)
	{
		return false;
	}
	return !strcmp( m_p, s.m_p);
}
bool MyString::operator!=( const char *p )
{
	return !(*this==p);
}
bool MyString::operator!=( const MyString &s )
{
	return !(*this == s);
}
好困,头有点晕,


然后是大于,小于比较,

int MyString::operator<( const char *p)
{	
	return strcmp( this->m_p, p);
}
int MyString::operator>( const char *p)
{
	return strcmp( p, this->m_p);
}
int MyString::operator<( const MyString &s)
{
	return strcmp( this->m_p, s.m_p);
}
int MyString::operator>( const MyString &s)
{
	return strcmp( s.m_p, this->m_p);
}



类中的私有变量,不能直接访问,

若是真的想直接访问,

可以写一个共有函数,返回那个私有变量的地址,

间接的修改,



istream& operator>>( istream &in,  MyString &s)
{
	cin>>s.m_p;
	return in;
}


最后要说的是,

实际中,是一边写测试案例,一边写实现,




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值