-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.19.delegate.constructor.cpp
executable file
·66 lines (57 loc) · 1.32 KB
/
2.19.delegate.constructor.cpp
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
//
// 2.19.constructor.cpp
// chapter 2 language usability
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//
#include <iostream>
#include <string>
class Base {
public:
std::string str;
int value;
Base() = delete;
Base(std::string s) {
str = s;
}
// delegate constructor
Base(std::string s, int v) : Base(s) {
value = v;
}
// final constructor
virtual void foo() final {
return;
}
virtual void foo(int v) {
value = v;
}
};
class Subclass final : public Base {
public:
double floating;
Subclass() = delete;
// inherit constructor
Subclass(double f, int v, std::string s) : Base(s, v) {
floating = f;
}
// explifict constructor
virtual void foo(int v) override {
std::cout << v << std::endl;
value = v;
}
}; // legal final
// class Subclass2 : Subclass {
// }; // illegal, Subclass has final
// class Subclass3 : Base {
// void foo(); // illegal, foo has final
// }
int main() {
// Subclass oops; // illegal, default constructor has deleted
Subclass s(1.2, 3, "abc");
s.foo(1);
std::cout << s.floating << std::endl;
std::cout << s.value << std::endl;
std::cout << s.str << std::endl;
}