实践如下:
#include <iostream> using namespace std; class Service { public: // 有一个虚函数即为抽象类 int id; // 不定义虚析构函数 会报右侧异常:@suppress("Class has a virtual method and non-virtual destructor") virtual ~Service(){} virtual double calc()=0; int aa(){ return 11; } }; class AService: public Service { public: //virtual ~AService(){} double calc() { // 操作抽象类中定义的字段 id = 100; cout << "id:" << id << endl; cout << "AService 100~~~" << endl; return 100; } }; class BService: public Service { public: //virtual ~BService(){} double calc() { cout << "BService 200~~~" << endl; return 200; } }; int main() { cout << "抽象类 实践:" << endl; Service *s = new AService(); s->calc(); cout << "\n抽象类 end." << endl; return 0; }
结果:
原文地址:https://www.cnblogs.com/do-your-best/p/11211179.html
时间: 2024-10-24 23:02:48