谁帮我做个C++编程问题?程序正确 !但是看不懂!望高手写下详细注释!

来源:百度知道 编辑:UC知道 时间:2024/05/18 09:35:32
#include<iostream>
using namespace std;
class MyString//声明一个类,类名为MyString
{
char cpBody[81];
public:

MyString(const char* p = NULL);

MyString(int i);

MyString& operator=(const MyString& s)

{ strncpy(cpBody, s.cpBody, 80);
return *this; }

MyString& operator[](int i);

MyString& operator+(int i);

MyString& operator++(int i);

void display() { printf("<%s>\n", cpBody); }

};
MyString::MyString(const char* p)
{
if (p == NULL)

cpBody[0] = '\0';

else

strncpy(cpBody, p, 80);

}
MyString::MyString(int i)
{
int j;

for (j = 0; j < i && j < 80; j++)

cpBody[j] = ' ';

cpBody[j] = '\0';

}
MyString& MyString::operator++(int i)
{

static MyString s; s = *this;

*this =

这个程序是对string中一些运算符的一个实现,对“=,+,++”运算符进行了重载。
我写的是说明,不是注释,你说看不懂,我就这样写了,而且你没说什么地方不懂,我就很啰嗦地基本每一行都写了。

#include<iostream>
using namespace std;
class MyString//声明一个类,类名为MyString
{
char cpBody[81];
public:

MyString(const char* p = NULL);//constructor ,定义了一个const的字符型指针

MyString(int i); //constructor,这两个是申明

MyString& operator=(const MyString& s)//对=重载,用了strncpy

{ strncpy(cpBody, s.cpBody, 80); //返回的是this指针
return *this; }

MyString& operator[](int i);//对[]的重载

MyString& operator+(int i); //对+的重载

MyString& operator++(int i); //对++的重载,这几个的定义都在后面

void display() { printf("<%s>\n", cpBody); }//定义一个函数 ,打出cpBody

};
MyString::MyString(const char* p) //现在是定义了
{
if (p == NULL)

cpBody[0] = '\0'; //定义cpBody为空

else

strncpy(cpBody, p, 80);//将p中前80位copy到cpBody中