WSS_ 2020-03-15 16:36 采纳率: 37.5%
浏览 393
已结题

求问,为什么没有输出???

#include
#include
using namespace std;

class myString

{
public:
myString(const char*pn=0);
myString(const myString &other);
~myString();
void set(const char *pn=0);
void print();

char get(int i);

void toUpper();

bool strcompare(const myString&s);

private:

char *pStr;
int size;

};

myString::myString(const char*pn)
{}

myString::myString(const myString &other)

{

size=other.size;

if(other.pStr)

{
    pStr=new char[strlen(other.pStr)+1];
    strcpy(pStr,other.pStr);
}
else
{
    pStr=new char[8];
    strcpy(pStr,"no name");
}

}
myString::~myString()
{

delete[]pStr;

}

void myString::set(const char*pn)
{

strcpy(pStr,pn);

}
void myString::print()
{
size=strlen(pStr);
for(int i=0;i<10;i++)
{
cout<<pStr[i]<<" "<<endl;;
}

cout<<size<<endl;

}
char myString::get(int i)
{
if(i<=size)
return pStr[i];
else
return 0;
}
void myString::toUpper()
{

}
bool myString::strcompare(const myString& s)
{

int m=strlen(s.pStr);
if(size=m) return true;
else return false;

}

int main()

{

myString s2,s1("Hello");
cout<<s1.get(0)<<endl;
s2.print();
s2.set("Hello world");
s2.print();
s1.toUpper();
s1.print();
myString s3=s2;
if(s2.strcompare(s3)) cout<<"字符串相同";
else cout<<"字符串不同";
return 0;

}

  • 写回答

2条回答 默认 最新

  • zx289544730 2020-03-15 18:55
    关注

    myString的构造函数myString(const char*pn)里没有赋值,
    myString(const myString &other)里的pStr new需要在myString(const char*pn)里做同样的处理,
    你写的s2,s1都是执行的myString(const char*pn)构造。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月7日
  • 已采纳回答 2月7日

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题