C++抽象类实践

Posted do-your-best

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++抽象类实践相关的知识,希望对你有一定的参考价值。

实践如下:

#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;

结果:

技术图片

 

以上是关于C++抽象类实践的主要内容,如果未能解决你的问题,请参考以下文章

C++面向对象:C++ 接口(抽象类)

工程实践:C++接口设计指北

C++ 抽象类二(抽象类的基本语法)

弄清楚是啥让 VS2008 中的 C++ 类抽象

c++ 数据抽象 封装 接口(抽象类)

c++纯虚函数和抽象类