zoukankan      html  css  js  c++  java
  • [C++基础] 纯虚函数

    整理摘自https://blog.csdn.net/ithomer/article/details/6031329

    1. 申明格式

    class CShape
    {
    public:
        virtual void Show()=0;
    };

    在普通的虚函数后面加上"=0"这样就声明了一个pure virtual function.

    2. 何时使用纯虚函数?

    (1)当想在基类中抽象出一个方法,且该基类只做能被继承,而不能被实例化;
    (2)这个方法必须在派生类(derived class)中被实现;
      如果满足以上两点,可以考虑将该方法申明为pure virtual function.

    3. 举例说明

    (1)不可实例化含有虚函数的基类

    我们来举个例子,我们先定义一个形状的类(Cshape),但凡是形状我们都要求其能显示自己。所以我们定义了一个类如下:

    class CShape
    {
        virtual void Show(){};
    };

    但没有CShape这种形状,因此我们不想让CShape这个类被实例化,我们首先想到的是将Show函数的定义(实现)部分删除如下:

    class CShape
    {
        virtual void Show();
    };
    当我们使用下面的语句实例化一个CShape时:
    CShape cs;  //这是我们不允许的,但仅用上面的代码是可以通过编译(但link时失败)。
     
    怎样避免一个CShape被实例化,且在编译时就被发现?
    答案: 使用pure virtual function.
    class CShape
    {
    public: virtual void Show()=0; };

    当实例化CShape cs 后,会报错:

    error: cannot declare variable ‘cs’ to be of abstract type ‘CShape’
    CShape cs;
                  ^
    note: because the following virtual functions are pure within ‘CShape’:
    class CShape
              ^
    note: virtual void CShape::Show()
    virtual void Show()= 0;

                      ^

    (2)派生类中堆基类中虚函数的实现

            我们再来看看被继承的情况,我们需要一个CPoint2D的类,它继承自CShape.它必须实现基类(CShape)中的Show()方法。
            其实使用最初的本意是让每一个派生自CShape的类,都要实现Show()方法,但时常我们可能在一个派生类中忘记了实现Show(),为了避免这种情况,pure virtual funcion发挥作用了。
    class CShape
    {
    public:
        virtual void Show()= 0;
    };
    
    class  CPoint2D: public CShape
    {
    public:
    
        void Msg()
        {
            printf("CPoint2D.Msg() is invoked.
    ");
        }
    /*
        void Show()
        {
            printf("Show() from CPoint2D.
    ");
        }
    */    
    };

    当实例化 CPoint2D p2d时,报错

    error: cannot declare variable ‘p2d’ to be of abstract type ‘CPoint2D’
    CPoint2D p2d;
                    ^
    note: because the following virtual functions are pure within ‘CPoint2D’:
    class CPoint2D: public CShape
             ^
    note: virtual void CShape::Show()
    virtual void Show()= 0;
                      ^

    我们预防了在派生类中忘记实现基类方法。如果不在派生类的中实现在Show方法,编译都不会通过。

    以下为完整代码:

    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    class CShape
    {
    public:
        virtual void Show()= 0;
    };
    
    class  CPoint2D: public CShape
    {
    public:
    
        void Msg()
        {
            printf("CPoint2D.Msg() is invoked.
    ");
        }
    
        void Show()
        {
            printf("Show() from CPoint2D.
    ");
        }
    };
    
    int main()
    {
        CPoint2D p2d;
        p2d.Msg();
        CPoint2D *pShape = &p2d;
        pShape -> Show();
        return 0;
    }
    
    /* Output:
    CPoint2D.Msg() is invoked.
    Show() from CPoint2D.
    */

  • 相关阅读:
    脑子好,蹦两下!程序员应该玩的小游戏
    推荐博客备份软件blog_bakcup
    经验(转)
    as super
    ActionScript工程如何使用Flash CS的fl包中的UI组件(转)
    astar(转)
    GROUPING,ROLLUP和CUBE(转)
    什么是分区表?为什么要用分区表?如何创建分区表?
    Flash player 详解(zhuan)
    jsfl(转)
  • 原文地址:https://www.cnblogs.com/shiyublog/p/9758577.html
Copyright © 2011-2022 走看看