zoukankan      html  css  js  c++  java
  • 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 << "
    抽象类 end." << endl;
    
        return 0;
    }

    结果:

  • 相关阅读:
    vim python extension
    aws msk
    Install python3
    sns
    inventory
    批量添加监听端口
    template screen
    DNS name
    add jar and proxy repo
    模型诊断论文心得
  • 原文地址:https://www.cnblogs.com/do-your-best/p/11211179.html
Copyright © 2011-2022 走看看