实践如下:
#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; }
结果: