#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class strin {
private:
char* s;
public:
strin(const char* m = NULL);
strin(const strin& other);
~strin();
strin& operator=(const strin& other);
void show()const {
cout << s << endl;
}
};
strin::~strin() {
delete s;
}
strin::strin(const char* m) {
if (m == 0) {
s = new char[1];
*s = '\0';
}
else {
int l = strlen(m);
s = new char[l + 1];
strcpy(s, m);
}
}
strin::strin(const strin& other) {
int l = strlen(other.s);
s = new char[l + 1];
strcpy(s, other.s);
}
strin& strin::operator=(const strin& other) {
if (this == &other) {
return *this;
}
else {
int l = strlen(other.s);
s = new char[l + 1];
strcpy(s, other.s);
return *this;
}
}
int main() {
strin t1("abc"), t2, t3("def"),t5;
t1.show(), t2.show(), t3.show();
t2 = t1;
t5.operator=(t1);
t5.show();
t2.show();
strin t4(t3);
t4.show();
}
编写并测试strin类的普通构造函数,复制构造函数,=重载函数,
最新推荐文章于 2024-08-12 14:11:13 发布