设计一个字符串类String(C++练习题)

要求:设计一个字符串类String,可以求字符串长度,可以连接两个串(如,s1=“计算机”,s2=“软件”,s1与s2连接得到“计算机软件”),并且重载“=”运算符进行字符串赋值,编写主程序实现:s1="计算机科学",s2=“是发展最快的科学!”,求s1和s2的串长,连接s1和s2

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std; class String
{ char str[]; public:
String(){str[]='\0';}
String(char* s){strcpy_s(str,s);}
String(String &s){strcpy_s(str,s.str);} String &operator=(String &);
String &operator=(char*); void display(){cout<<str<<endl;}
void strPlus(String);
int strLen(){return strlen(str);} };
String& String::operator =(String &s)
{
if(this==&s)return *this;
strcpy_s(str,s.str);
return *this; }
String& String::operator =(char *s)
{
strcpy_s(str,s);
return *this;
} void String::strPlus(String s)
{
int i=strLen();
int j=s.strLen();
int k=;
while(k<=j)
{
str[i++]=s.str[k++];
}
} int _tmain()
{
String s1("计算机");
String s2("软件");
s1.strPlus(s2);
s1.display();
String s3="计算机科学";
String s4="是发展最快的科学!";
cout<<"求串长:计算机科学="<<s3.strLen()<<endl;
cout<<"求串长:是发展最快的科学!="<<s4.strLen()<<endl;
s3.strPlus(s4);
s3.display();
getchar();
return ;
}
上一篇:Spring/SpringMVC/MyBatis(持久层、业务层、控制层思路小结)


下一篇:meterpreter命令大全