急求c++程序设计题答案!!!

2024-12-03 09:43:13
推荐回答(1个)
回答1:

#include
#include

using namespace std;

class Words
{
public:
Words(const char *w)
{
len=strlen(w);
word=new char[len+1];
strcpy(word,w);
}
~Words()
{
delete word;
}
int length() const
{
return len;
}
char& operator [](int i)
{
if(i>=0 && i return *(word+i);
}
const char& operator [](int i) const
{
if(i>=0 && i return *(word+i);
}
private:
int len;
char *word;
};

int main()
{
string word;

cout<<"请输入一个英文单词:";
cin>>word;
Words w1(word.c_str());
const Words w2(word.c_str());
for(int i=0;i cout< cout< w1[0]='S';
for(int i=0;i cout< cout< for(int i=0;i cout< cout< //w2[0]='S';
//for(int i=0;i // cout< //cout< return 0;
}