别摸我的键盘 的学生作业:
//
// Created by linux on 25-3-5.
//
#include
#include
#include
using namespace std;
class String {
public:
String(const char *str = nullptr) {
if (str == nullptr) {
this->str = new char[1];
this->str[0] = '\0';
} else {
this->str = new char[strlen(str) + 1];
strcpy(this->str, str);
}
cout = 0 && num < static_cast(size()));
return str[num];
}
/**
* 非const 版本 允许修改
* @param num
* @return
*/
char& operator[](int num){
assert(num >= 0 && num < static_cast(size()));
return str[num];
}
~String() {
delete[] str;
}
/**
* 输出字符串中的每个字符和对应的ASCII码
*/
void show(void) {
if (this->str == nullptr) {
return;
}
const char *ptr = str;
while (*ptr != '\0') {
cout