-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy path0.cpp
More file actions
81 lines (63 loc) · 2.22 KB
/
Copy path0.cpp
File metadata and controls
81 lines (63 loc) · 2.22 KB
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
// d2mcpp: https://github.com/mcpp-community/d2mcpp
// license: Apache-2.0
// file: src/cpp11/tests/10-delegating-constructors/0.cpp
//
// Exercise/练习: cpp11 | 10 - delegating constructors | 委托构造函数
//
// Tips/提示: 根据编译器的输出, 修复编译器报错, 了解委托构造函数的基本使用
//
// Docs/文档:
// - https://en.cppreference.com/w/cpp/language/initializer_list.html#Delegating_constructor
// - https://github.com/mcpp-community/d2mcpp/blob/main/book/src/cpp11/10-delegating-constructors.md
//
// Auto-Checker/自动检测命令:
//
// d2x checker delegating-constructors
//
import std;
import d2x;
static int construction_counter { 0 };
class Account {
std::string id;
std::string name;
std::string coin;
public:
Account(std::string id_) {
id = id_;
name = "momo";
coin = "0元";
d2x::dont_delete_this(construction_counter++);
}
Account(std::string id_, std::string name_) {
id = id_;
name = name_;
coin = "0元";
d2x::dont_delete_this(construction_counter++);
}
Account(std::string id_, std::string name_, int coin_) {
id = id_;
name = name_;
coin = std::to_string(coin_) + "元";
d2x::dont_delete_this(construction_counter++);
}
std::string to_string() const {
return "Account { id: " + id + ", name: " + name + ", coin: " + coin + " }";
}
};
int main() { // 不要修改main函数中的代码
Account a1 { "1111" };
d2x::check_eq(construction_counter, 3, "construction_counter == 3");
std::cout << a1.to_string() << std::endl;
Account a2 { "2222", "wukong" };
d2x::check_eq(construction_counter, 5, "construction_counter == 5");
std::cout << a2.to_string() << std::endl;
Account a3 { "3333", "mcpp", 100 };
d2x::check_eq(construction_counter, 6, "construction_counter == 6");
std::cout << a3.to_string() << std::endl;
Account gi { "0000", "GImpact", 648 };
std::cout << gi.to_string() << std::endl;
d2x::check(gi.to_string() ==
"Account { id: 0000, name: GImpact, coin: 648原石 }", "gi.to_string() == \"Account { id: 0000, name: GImpact, coin: 648原石 }\"");
d2x::wait();
return 0;
}