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
|
#include<iostream>
using namespace std;
class Base {
public:
virtual void print() {
cout << "This is Base class." << endl;
}
};
class Derived : public Base {
public:
void print() {
cout << "This is Derived class." << endl;
}
};
int main() {
Derived derivedObj;
Base& baseRef = derivedObj; // 使用基类引用引用派生类对象
baseRef.print(); // 调用的是 Derived 类中的 print 函数
return 0;
}
|