string.cpp

来自「二叉树的 学生成绩管理系统 c」· C++ 代码 · 共 70 行

CPP
70
字号
#include "String.h"

String::String(const String & ob)
{
	curlen=ob.curlen;
	ch=new char[curlen];
	assert(ch!=NULL);
	strcpy(ch,ob.ch);
}

String::String(char *init)
{
	curlen=strlen(init)+1;
	ch=new char[curlen];
	assert(ch!=NULL);
	strcpy(ch,init);
}

String & String::operator =(const String & ob)
{
	if(curlen!=ob.curlen)
	{
		delete []ch;
		ch=new char[ob.curlen];
		if(ch==NULL)
		{
			cout<<"Invalid Error"<<endl;
			exit(1);
		}
		curlen=ob.curlen;
	}
	strcpy(ch,ob.ch);
	return *this;
}

String & String::operator =(char *s)
{
	int len=strlen(s)+1;
	if(len!=curlen)
	{
		delete []ch;
		ch=new char[strlen(s)+1];
		if(ch==NULL)
		{
			cout<<"Invalid Error"<<endl;
			exit(1);
		}
		curlen=strlen(s)+1;
	}
	strcpy(ch,s);
	return *this;
}

ostream & operator <<(ostream & os,const String & s)
{
	os<<s.ch;
	return os;
}

istream & operator >>(istream & is,String & s)
{
	s.ch=new char[maxlen];
	if(s.ch==NULL)
	{
		cerr<<"Invalid Error"<<endl;
		exit(1);
	}
	is>>s.ch;
	return is;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?