题目内容:
说明以下类Words的功能,并给出程序执行结果。#include<iostream.h>
#include<string.h>
class Words
{
char *str;
public:
Words(char *s)
{
str=new char[strlen(s)+1];
strcpy(str,s);
}
void disp(){cout<<str<<endl;}
char operator[](int i)
{
if(str[i]>=’A’&& str[i]<='Z') // 大写字母
return char(str[i]+32);
else if(str[i]>='a'&&str[i]<='z') // 4'写字母
return char(str[i]-32);
else // 其他字符
return str[i];
}
}
void main()
{
int i;
char *s=”Hello”;
Words word(s);
word.disp();
for(i=0;i<strlen(s);i++)
cout<<word[i];
cout<<endl:
}
参考答案:
答案解析: