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

34 lines
448 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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;
}