include
#include
class MString{
char *s;
int len;
public:
MStirng();
MString(char *ps);
MString(const MString &t);//copy funcation
~MString();
int size();
void swap(MString &t);
void find(char);
void find(MString &t);
MString &strcats(MString& t);
MString &strcpys(MString& t);
};
MString& MString::strcats(MString& t){
char *p;
p=new char[len+t.len+1];
strcpy(p,s);
strcat(p,t.s);
delete[]s;
s=p;
len+=t.len;
return *this;}
MString& Mstring::strcpys(MString& t){
s[1]=strcpy(s[0]);
}
MString::~MString()
int MString::size(int len){
for(int i=0; i<*s;i++);
cout<<s[i]<<" ";
}
void MString::find(char){
if(s.find(s[1])!=-1)
count <<"find"<<s[0]<<endl;
}
void MString::swap(MString &t){
s[0].swap(s[1]);
}
int main{
MString t("I am a student","teacher")
MString p=s;
}
}
s[1]=strcpy(s[0]);
->
strcpy(s[0], t.s[0]);
return &this;
MString t("I am a student","teacher")
->
MString t("I am a student","teacher");
修改后的
#include <string>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
class MString{
char *s;
int len;
public:
//MStirng();
MString(char *ps);
MString(const MString &t);//copy funcation
~MString();
int size();
void swap(MString &t);
void find(char);
void find(MString &t);
MString &strcats(MString& t);
MString &strcpys(MString& t);
friend ostream& operator <<(ostream &os, const MString &str);
};
MString::MString(char *ps)
{
len = strlen(ps)+1;
s = new char[len];
strcpy(s, ps);
}
MString& MString::strcats(MString& t)
{
char *p;
p=new char[len + t.len + 1];
strcpy(p,s);
strcat(p, t.s);
delete s;
s=p;
len+=t.len;
return *this;
}
MString& MString::strcpys(MString& t)
{
strcpy(s, t.s);
return *this;
}
MString::~MString()
{
cout<<"MString descontruct"<<endl;
delete s;
}
int MString::size()
{
return len;
}
void MString::find(char c){
char* k;
k = std::find(s, s+len, c);
if(k != s+len)
cout <<"find"<<*k<<endl;
else
cout << "not find char "<< c <<endl;
}
void MString::swap(MString &t){
std::swap(s, t.s);
}
ostream& operator <<(ostream &os, const MString &str)
{
os<<str.s<<endl;
return os;
}
int main(void){
MString t("I am a student");
//MString p=s;
MString s("I am a teacher");
cout<<t;
t.strcpys(s);
cout<<t;
t.strcats(s);
cout<<t;
system("pause");
return 0;
}