-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
47 lines (40 loc) · 1.14 KB
/
test.cpp
File metadata and controls
47 lines (40 loc) · 1.14 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
#include <iostream>
using namespace std;
class Base {
public:
virtual void publicVirtualFunction() {
// 虚函数的公有实现
cout << "publicVirtualFunction in base " << endl;
}
protected:
virtual void protectedVirtualFunction() {
// 虚函数的受保护实现
cout << "protectedVirtualFunction in base " << endl;
}
private:
virtual void privateVirtualFunction() {
// 虚函数的私有实现
}
};
class Derived : public Base {
public:
void publicVirtualFunction() override {
// 派生类覆盖了基类的公有虚函数
cout << "publicVirtualFunction in derived " << endl;
}
protected:
void protectedVirtualFunction() override {
// 派生类覆盖了基类的受保护虚函数
cout << "protectedVirtualFunction in derived " << endl;
}
private:
void privateVirtualFunction() override {
// 派生类无法访问基类的私有虚函数
}
};
int main() {
Base* basePtr = new Derived;
basePtr->publicVirtualFunction(); // 通过基类指针调用公有虚函数
basePtr->protectedVirtualFunction();
return 0;
}