-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy path11-inherited-constructors-0.cpp
More file actions
72 lines (55 loc) · 1.88 KB
/
11-inherited-constructors-0.cpp
File metadata and controls
72 lines (55 loc) · 1.88 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
// d2mcpp: https://github.com/mcpp-community/d2mcpp
// license: Apache-2.0
// file: dslings/cpp11/11-inherited-constructors-0.cpp
//
// Exercise/练习: cpp11 | 11 - inherited constructors | 继承构造函数
//
// Tips/提示: 根据编译器的输出和报错信息, 添加符合要求的构造函数, 并了解继承构造函数的基本用法
//
// Docs/文档:
// - https://en.cppreference.com/w/cpp/language/using_declaration.html#Inheriting_constructors
// - https://mcpp-community.github.io/d2mcpp/cpp11/11-inherited-constructors.html
//
// Auto-Checker/自动检测命令:
//
// d2x checker inherited-constructors
//
#include <d2x/cpp/common.hpp>
#include <iostream>
#include <string>
class ObjectBase {
public:
ObjectBase(int x) { std::cout << "ObjectBase::ObjectBase(int): " << x << std::endl; }
ObjectBase(double x) { std::cout << "ObjectBase::ObjectBase(double): " << x << std::endl; }
D2X_YOUR_ANSWER
void info() const { std::cout << "ObjectBase: " << this << std::endl; }
};
class ObjectA : public ObjectBase {
public:
ObjectA(int x) : ObjectBase(x) { std::cout << "ObjectA::ObjectA(int)" << std::endl; }
ObjectA(double y) : ObjectBase(y) { std::cout << "ObjectA::ObjectA(double)" << std::endl; }
D2X_YOUR_ANSWER
void tips_a() const {
std::cout << "ObjectA: add constructors to ObjectA" << std::endl;
}
};
class ObjectB : public ObjectBase {
public:
using ObjectBase::ObjectBase;
// D2X_YOUR_ANSWER ?
void tips_b() const {
std::cout << "ObjectB: add new constructors to ObjectBase" << std::endl;
}
};
int main() { // 不要直接修改 main 函数中的代码
ObjectBase obj1(1), obj2(2.0), obj3 { 3, 4.0 };
ObjectA a1(11), a2(22.0), a3 { 33, 44.0 };
ObjectB b1(111), b2(222.0), b3 { 333, 444.0 };
obj1.info();
a1.info();
b1.info();
a1.tips_a();
b1.tips_b();
D2X_WAIT
return 0;
}