qfedu-cpp-level/day8/homework/h1.cpp

34 lines
448 B
C++
Raw Normal View History

2023-08-02 20:48:31 +08:00
// const修饰的成员虚函数能否在子类中重写请举例说明
// 可以
#include <bits/stdc++.h>
using namespace std;
class A
{
public:
virtual void show() const
{
cout << "A" << endl;
}
};
class B : public A
{
public:
virtual void show() const override
{
cout << "B" << endl;
}
};
int main()
{
A *p = new B;
p->show(); // B
B *q = new B;
q->show(); // B
return 0;
}