c++纯虚函数和抽象类

2022-11-24  本文已影响0人  arkliu

纯虚函数

语法:

virtual 返回值类型 函数名(参数列表)=0;

抽象类

#include <iostream>
#include<string>
using namespace std;

class Parent{
    public:
        virtual void show()=0;
        Parent() {cout <<"Parent 构造函数.."<<endl;}
        ~Parent()=0; {cout <<"Parent 析构函数.."<<endl;}
};

class Child:public Parent {
    public:
        void show() {
            cout <<"Child::show()"<<endl;
        }
        Child() {cout <<"Child 构造函数.."<<endl;}
        ~Child() {cout <<"Child 析构函数.."<<endl;}
};

int main() {
    Child child;
    Parent *par = &child;
    par->show();
    return 0;   
}
image.png
上一篇 下一篇

猜你喜欢

热点阅读