zoukankan      html  css  js  c++  java
  • C++虚函数

    前言 

    虚函数是在类中被声明为virtual的成员函数,当编译器看到通过指针或引用调用此类函数时,对其执行晚绑定,即通过指针(或引用)指向的类的类型信息来决定该函数是哪个类的。通常此类指针或引用都声明为基类的,它可以指向基类或派生类的对象。通过类继承和虚函数机制可以实现C++运行期的动态多态。

    虚函数

    虚函数是动态多态性的基础,其调用的方式是动态联编(又称晚期联编,简单解释为只有在程序运行时才决定调用基类的还是子类的,系统会根据基类指针所指向的对象来决定要调用的函数)。
    非虚函数与其相反,是静态联编(调用已经在编译时期就决定了;在编译时期,系统已经根据指针所属的类型确定了要调用的函数)。

    在定义了虚函数后,可以在基类的派生类中对虚函数重新定义,在派生类中重新定义的函数应与虚函数具有相同的形参个数和形参类型。以实现统一的接口,不同定义过程。如果在派生类中没有对虚函数重新定义,则它继承其基类的虚函数,例子如下:

    // virtual members
    #include <iostream>
    using namespace std;

    class CPolygon {
    protected:
    int width, height;
    public:
    void set_values (int a, int b)
    { width=a; height=b; }
    virtual int area ()
    { return (0); }
    };

    class CRectangle: public CPolygon {
    public:
    int area ()
    { return (width * height); }
    };

    class CTriangle: public CPolygon {
    public:
    int area ()
    { return (width * height / 2); }
    };

    int main () {
    CRectangle rect;
    CTriangle trgl;
    CPolygon poly;
    CPolygon * ppoly1 = &rect;
    CPolygon * ppoly2 = &trgl;
    CPolygon * ppoly3 = &poly;
    ppoly1->set_values (4,5);
    ppoly2->set_values (4,5);
    ppoly3->set_values (4,5);
    cout << ppoly1->area() << endl;
    cout << ppoly2->area() << endl;
    cout << ppoly3->area() << endl;
    return 0;
    }

    纯虚函数

    如果父类的函数(方法)根本没有必要或者无法实现,完全要依赖子类去实现的话,可以把此函数(方法)设为virtual 函数名=0 我们把这样的函数(方法)称为纯虚函数。
    如果一个类包含了纯虚函数,称此类为抽象类,抽象类是不能实例化的。

    一个抽象基类CPolygon可能看起来像这样:

    // abstract class CPolygon
    class CPolygon {
    protected:
    int width, height;
    public:
    void set_values (int a, int b)
    { width=a; height=b; }
    virtual int area () =0;
    };

    一个类不能实例化对象,不是完全没有用处我们可以创建指针充分利用它多态能力因此,声明如下

    CPolygon * ppoly1;
    CPolygon * ppoly2;

    CPolygon包含一个纯虚函数,因此它是一个抽象基类然而,这个抽象基类指针可以用来指向派生类的对象

    完整示例如下:

    // abstract base class
    #include <iostream>
    using namespace std;

    class CPolygon {
    protected:
    int width, height;
    public:
    void set_values (int a, int b)
    { width=a; height=b; }
    virtual int area (void) =0;
    };

    class CRectangle: public CPolygon {
    public:
    int area (void)
    { return (width * height); }
    };

    class CTriangle: public CPolygon {
    public:
    int area (void)
    { return (width * height / 2); }
    };

    int main () {
    CRectangle rect;
    CTriangle trgl;
    CPolygon * ppoly1 = &rect;
    CPolygon * ppoly2 = &trgl;
    ppoly1->set_values (4,5);
    ppoly2->set_values (4,5);
    cout << ppoly1->area() << endl;
    cout << ppoly2->area() << endl;
    return 0;
    }





  • 相关阅读:
    [Angular 9] Built-in template syntax $any
    [Angular 9] Improved Dependency Injection with the new providedIn scopes 'any' and 'platform'
    [Angular 9 Unit testing] Stronger typing for dependency injection in tests
    [Angular] Preserve the current route’s query parameters when navigating with the Angular Router
    [Angular] Do relative routing inside component
    [Typescript] Make your optional fields required in TypeScript
    [Typescript] Exclude Properties from a Type in TypeScript
    [Javascript] Hide Properties from Showing Up in "for ... in" Loops in JavaScript
    [Debug] Set and remove DOM breakpoints
    【职业素养】4种让你显得没教养的做法
  • 原文地址:https://www.cnblogs.com/ggjucheng/p/2310487.html
Copyright © 2011-2022 走看看