实践如下:

#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()=;
int aa(){
return ;
}
}; class AService: public Service {
public:
//virtual ~AService(){}
double calc() {
// 操作抽象类中定义的字段
id = ;
cout << "id:" << id << endl; cout << "AService 100~~~" << endl;
return ;
}
};
class BService: public Service {
public:
//virtual ~BService(){}
double calc() {
cout << "BService 200~~~" << endl;
return ;
}
}; int main() { cout << "抽象类 实践:" << endl; Service *s = new AService();
s->calc(); cout << "\n抽象类 end." << endl; return ;
}

结果:

C++抽象类实践-LMLPHP

05-25 17:23