C++小题一道·· 求改正·······························
#include<iostream>#include<cstring>usingnamespacestd;classString{private:char*_str;pu...
#include<iostream>
#include<cstring>
using namespace std;
class String
{
private:
char* _str;
public:
/* String operator=(char* p)
{
int s=strlen(p);
_str=new char[s+1];
strcpy(_str,p);
return *this;
}*/
String& operator=(String other)
{ delete[] _str;
if (other._str == NULL) _str = NULL;
else
{ _str = new char[strlen(other._str) + 1];
strcpy(_str, other._str);
}
return *this;
}
String operator+=(char* str)
{
int len=strlen(_str)+strlen(str);
if(len==0)
return *this;
char* buffer=new char[len+1];
strcpy (buffer,_str);
strcat(buffer,str);
_str=buffer;
return *this;
}
bool operator!=(String x)
{
if(strcmp(x._str,_str)==0)
{ return true;}
else return false;
}
friend ostream operator<<(ostream& out,const String& o)
{
out<<o._str<<endl;
return out;
}
};
void main()
{
String a = "good student";
String b = "good";
b += "student";
if (a != b) cout<<"not same"<<endl;
char str[100];
strcpy(str, (const char*)a);
cout<<str<<endl;
} 展开
#include<cstring>
using namespace std;
class String
{
private:
char* _str;
public:
/* String operator=(char* p)
{
int s=strlen(p);
_str=new char[s+1];
strcpy(_str,p);
return *this;
}*/
String& operator=(String other)
{ delete[] _str;
if (other._str == NULL) _str = NULL;
else
{ _str = new char[strlen(other._str) + 1];
strcpy(_str, other._str);
}
return *this;
}
String operator+=(char* str)
{
int len=strlen(_str)+strlen(str);
if(len==0)
return *this;
char* buffer=new char[len+1];
strcpy (buffer,_str);
strcat(buffer,str);
_str=buffer;
return *this;
}
bool operator!=(String x)
{
if(strcmp(x._str,_str)==0)
{ return true;}
else return false;
}
friend ostream operator<<(ostream& out,const String& o)
{
out<<o._str<<endl;
return out;
}
};
void main()
{
String a = "good student";
String b = "good";
b += "student";
if (a != b) cout<<"not same"<<endl;
char str[100];
strcpy(str, (const char*)a);
cout<<str<<endl;
} 展开
展开全部
#include<iostream>
#include<cstring>
using namespace std;
class String
{
private:
char* _str;
public:
String(const char* arr){
int s=strlen(arr);
_str=new char[s+1];
strcpy(_str,arr);
}
operator const char*() const{
return _str;
}
String& operator=(char* p)
{
int s=strlen(p);
_str=new char[s+1];
strcpy(_str,p);
return *this;
}
String& operator=(String other)
{
delete[] _str;
if (other._str == NULL) _str = NULL;
else
{
_str = new char[strlen(other._str) + 1];
strcpy(_str, other._str);
}
return *this;
}
String operator+=(char* str)
{
int len=strlen(_str)+strlen(str);
if(len==0)
return *this;
char* buffer=new char[len+1];
strcpy (buffer,_str);
strcat(buffer,str);
_str=buffer;
return *this;
}
bool operator!=(const String& x)
{
if(strcmp(x._str,_str)==0)
{ return true;}
else return false;
}
friend ostream& operator<<(ostream& out,const String& o)
{
out<<o._str<<endl;
return out;
}
};
void main()
{
String a = "good student";
String b = "good";
b += "student";
if (a != b) cout<<"not same"<<endl;
char str[100];
strcpy(str, (const char*)a);
cout<< str <<endl;
}
#include<cstring>
using namespace std;
class String
{
private:
char* _str;
public:
String(const char* arr){
int s=strlen(arr);
_str=new char[s+1];
strcpy(_str,arr);
}
operator const char*() const{
return _str;
}
String& operator=(char* p)
{
int s=strlen(p);
_str=new char[s+1];
strcpy(_str,p);
return *this;
}
String& operator=(String other)
{
delete[] _str;
if (other._str == NULL) _str = NULL;
else
{
_str = new char[strlen(other._str) + 1];
strcpy(_str, other._str);
}
return *this;
}
String operator+=(char* str)
{
int len=strlen(_str)+strlen(str);
if(len==0)
return *this;
char* buffer=new char[len+1];
strcpy (buffer,_str);
strcat(buffer,str);
_str=buffer;
return *this;
}
bool operator!=(const String& x)
{
if(strcmp(x._str,_str)==0)
{ return true;}
else return false;
}
friend ostream& operator<<(ostream& out,const String& o)
{
out<<o._str<<endl;
return out;
}
};
void main()
{
String a = "good student";
String b = "good";
b += "student";
if (a != b) cout<<"not same"<<endl;
char str[100];
strcpy(str, (const char*)a);
cout<< str <<endl;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
错误太多了:首先缺少构造函数,所以
String a = "good student";
String b = "good";
这两句程序是不正确的,通过不了编译,英爱构造自己的构造函数:String(){......}
String a = "good student";
String b = "good";
这两句程序是不正确的,通过不了编译,英爱构造自己的构造函数:String(){......}
追问
String a = "good student";
String b = "good";
你好,这两句是题目给的,就是要实现这个=的赋值·· 用重载····
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
哥们 。 你自己好好检查一下输入是否正确 我看见了好几个 你自己吧单词打错了 少一些字母。。
追问
嘿嘿,新手新手,但是我又看了两遍· 还是·····,后生愚钝··请明示···
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询