forked from mcpp-community/d2mcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-final-and-override-0.cpp
More file actions
57 lines (45 loc) · 1.11 KB
/
02-final-and-override-0.cpp
File metadata and controls
57 lines (45 loc) · 1.11 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
// mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
// license: Apache-2.0
// file: dslings/cpp11/02-final-and-override-0.cpp
//
// Exercise/练习: cpp11 | 02 - final and override
//
// Tips/提示: 修正代码中override的使用错误
//
// Docs/文档:
// - https://en.cppreference.com/w/cpp/language/final
// - https://en.cppreference.com/w/cpp/language/override
//
// Auto-Checker/自动检测命令:
//
// d2x checker final-and-override
//
#include <d2x/common.hpp>
#include <iostream>
#include <string>
struct A {
virtual void func1() {
std::cout << "A::func1()" << std::endl;
}
void func2() {
std::cout << "A::func2()" << std::endl;
}
};
struct B : A {
void func1() {
std::cout << "B::func1()" << std::endl;
}
void func2() override {
std::cout << "B::func2()" << std::endl;
}
};
int main() {
B override; // 不要直接修改main函数中的代码
override.func1(); // B::func1()
override.func2(); // B::func2()
A *a = &override;
a->func1(); // B::func1()
a->func2(); // A::func2()
D2X_WAIT
return 0;
}