cpp-实验(wust

1th

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
using namespace std;

class Date {
private:
int year;
int month;
int day;
public:
Date(int y = 2006, int m = 3, int d = 3) : year(y), month(m), day(d) {
cout << "Date的默认构造" << endl;
}
Date(const Date& tmp) : year(tmp.year), month(tmp.month), day(tmp.day) {
cout << "Date的复制构造" << endl;
}
~Date() {
cout << "Date的析构函数" << endl;
}
void Show() const {
cout << year << "-" << month << "-" << day << endl;
}
void Set(int y, int m, int d) {
year = y;
month = m;
day = d;
}
};

class Staff {
private:
int num;
string name;
char gender;
Date birth;
string id;
public:
Staff(int nu = 0, const string& na = "scandi", char g = 'M',
const Date& d = Date(), const string& i = "100000000000000000")
: num(nu), name(na), gender(g), birth(d), id(i) {
cout << "Staff的构造函数" << endl;
}
Staff(const Staff& s)
: num(s.num), name(s.name), gender(s.gender), birth(s.birth), id(s.id) {
cout << "Staff的复制构造" << endl;
}
~Staff() {
cout << "Staff的析构函数" << endl;
}
inline void Show_Staff() const {
cout << num << endl;
cout << name << endl;
cout << gender << endl;
birth.Show();
cout << id << endl;
}
inline void Set_Staff(int nu, const string& na, char g, const Date& d, const string& i) {
num = nu;
name = na;
gender = g;
birth = d;
id = i;
}
};

int main() {
Staff s;
s.Show_Staff();
return 0;
}

2th

1
出错了(流汗。。。

3th

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <cstring>
using namespace std;

class String {
private:
char* s = nullptr;
public:
friend istream& operator>> (istream& cin, String& x);
friend ostream& operator<< (ostream& cout, String& x);
String() :s(nullptr) {
cout << "String的构造函数" << endl;
}
String(const char* p) {
s = new char[strlen(p) + 1];
strcpy(s, p);
}
String(const String& x) {
if (s != nullptr) {
delete[] s;
}
s = new char[strlen(x.s) + 1];
strcpy(s, x.s);
}
~String() {
if (s != nullptr) {
delete[] s;
}
}
String operator+ (const String& x) {
String tmp;
if (x.s == nullptr) {
return *this;
}
tmp.s = new char(strlen(x.s) + strlen(s) + 1);
strcpy(tmp.s, s);
strcat(tmp.s, x.s);
return tmp;
}
String& operator+= (const String& x) {
const char* tt = s ? s : "";
char* tmp = new char[strlen(tt) + strlen(x.s) + 1];
strcpy(tmp, s);
strcat(tmp, x.s);
if (s != nullptr) {
delete[] s;
}
s = tmp;
return *this;
}
char& operator[] (int i) {
return s[i];
}
bool operator== (const String& x) {
return strcmp(this->s, x.s) == 0;
}
bool operator< (const String& x) {
return strcmp(this->s, x.s) < 0;
}
int Length() {
return strlen(s);
}
};
istream& operator>> (istream& cin, String& x) {
string tmp;
cin >> tmp;
if (x.s != nullptr) {
delete[] x.s;
}
x.s = new char[tmp.length() + 1];
strcpy(x.s, tmp.c_str());
return cin;
}
ostream& operator<< (ostream& cout, String& x) {
cout << (x.s ? x.s : "");
return cout;
}

int main(){
String s1("Help!"), s2("Good!"), s3(s2), s4, s5;
cout << "s1=" << s1 << endl;
s3 = "Hello!";
cout << "s3=" << s3 << endl;
s3 += s2;
cout << "s3=" << s3 << endl;
cin >> s4;
cout << "s4=" << s4 << endl;
s5 = s3 + s4;
cout << "s5=" << s5 << endl;
s5[0] = 'g';
cout << "s5=" << s5 << endl;
cout << "strlen(s5)=" << s5.Length() << endl;
cout << boolalpha << (s3 == s1) << endl;
return 0;
}