抽象类中可以对虚函数进行实现,但子类必须要重新该函数

廉价感情. 提交于 2019-12-04 14:02:51
抽象类中可以对虚函数进行实现,但子类必须要重新该函数,否则子类仍然是一个抽象类,不能实例化。子类中调用基类的方法可以用基类名::方法.#include <iostream>
using namespace std;

class A
{
public:
    virtual void display()=0
    {
        cout << "A" << endl;
    }
};

class B :public A
{
public:
    virtual void display()
    {
        A::display();
        cout << "B" << endl;
    }
};

int main()
{
    B b;
    b.display();
    return 0;
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!