C/C++中虚函数详解及其作用介绍

目录概述使用方法关联静态关联动态关联案例1未使用虚函数使用虚拟类案例2总结

概述

虚函数 (virtual function) 指可以被子类继承和覆盖的函数.

使用方法

基类声明成员函数为虚函数的方法:

virtual [类型] 函数名([参数表列])

注: 在类外定义虚函数时, 不需再加 virtual.

虚函数的特点:

提高程序扩充性: 派生类根据需要可以进行函数覆盖 成员函数被声明为虚数后, 其派生类中覆盖函数自动称为虚函数 若虚函数在派生类中未重新定义, 则派生类简单继承其直接基类的虚函数 指向基类的指针, 当指向派生类对象时, 可以嗲用派生类的方法

关联

通过关联 (binding), 我们可以把一个标识符和一个存储地址联系起来, 或者把一个函数名与一个类对象捆绑在一起.

静态关联

静态关联 (static binding) 指通过对象名调用虚函数. 在编译时即可确定其调用的虚函数属于哪一类

动态关联

动态关联 (dynamic binding) 是指通过基类指针与虚函数, 在运行阶段确定关联关系. 动态关联提供动态的多态性, 即运行阶段的多态性.

案例1

未使用虚函数

Square 类:

#ifndef PROJECT6_SQUARE_H#define PROJECT6_SQUARE_Hclass Square {protected:    int length;public:    Square(int l) : length(l) {};    int area() const {        return length *length;    }};#endif //PROJECT6_SQUARE_H

Rectangle 类:

#ifndef PROJECT6_RECTANGLE_H#define PROJECT6_RECTANGLE_H#include "Square.h"class Rectangle : public Square{private:    int height;public:    Rectangle(int l, int h) : Square(l), height(h) {};    int area() const {        return Square::area() * 2 + length * height * 4;  // 两个底加四个边    }};#endif //PROJECT6_RECTANGLE_H

main:

#include <iostream>#include "Square.h"#include "Rectangle.h"using namespace std;int main() {    // 创建对象    Square s1(2), *pt;    Rectangle r1(3, 3);    pt = &s1;    cout << pt->area() << endl;    pt = &r1;    cout << pt->area() << endl;    return 0;}

输出结果:

49 // 输出的是底面积

此时调用的是 Square 类的area()函数.

使用虚拟类

Square 类:

#ifndef PROJECT6_SQUARE_H#define PROJECT6_SQUARE_Hclass Square {protected:    int length;public:    Square(int l) : length(l) {};    virtual int area() const {        return length *length;    }};#endif //PROJECT6_SQUARE_H

Rectangle 类:

#ifndef PROJECT6_RECTANGLE_H#define PROJECT6_RECTANGLE_H#include "Square.h"class Rectangle : public Square{private:    int height;public:    Rectangle(int l, int h) : Square(l), height(h) {};    int area() const {        return Square::area() * 2 + length * height * 4;  // 两个底加四个边    }};#endif //PROJECT6_RECTANGLE_H

main:

#include <iostream>#include "Square.h"#include "Rectangle.h"using namespace std;int main() {    // 创建对象    Square s1(2), *pt;    Rectangle r1(3, 3);    pt = &s1;    cout << pt->area() << endl;    pt = &r1;    cout << pt->area() << endl;    return 0;}

输出结果:

454 // 长方体的面积

此时调用的是 Rectangle 类的area()函数.

案例2

Animal 类:

#ifndef PROJECT6_ANIMAL_H#define PROJECT6_ANIMAL_H#include <iostream>using namespace std;class Animal {public:    virtual void bark(){        cout << "咋叫?" << endl;    }};#endif //PROJECT6_ANIMAL_H

Dog 类:

#ifndef PROJECT6_DOG_H#define PROJECT6_DOG_H#include "Animal.h"class Dog : public Animal{public:    void bark() {        cout << "汪汪!" << endl;    }};#endif //PROJECT6_DOG_H

Cat 类:

#ifndef PROJECT6_CAT_H#define PROJECT6_CAT_H#include "Animal.h"class Cat : public Animal{public:    void bark() {        cout << "喵喵!" << endl;    }};#endif //PROJECT6_CAT_H

Pig 类:

#ifndef PROJECT6_PIG_H#define PROJECT6_PIG_H#include "Animal.h"class Pig : public Animal {public:    void bark() {        cout << "哼哼!" << endl;    }};#endif //PROJECT6_PIG_H

main:

#include <iostream>#include "Animal.h"#include "Dog.h"#include "Cat.h"#include "Pig.h"using namespace std;int main() {    // 创建对象    Animal a, *pt;    Dog d;    Cat c;    Pig p;    pt = &a;    pt -> bark();  // 调用基类的bark()    pt = &d;    pt -> bark();  // 调用狗的bark()    pt = &c;    pt -> bark();  // 调用猫的bark()    pt = &p;    pt -> bark();  // 调用猪的bark()    return 0;}

输出结果:

咋叫?汪汪!喵喵!哼哼!

总结

虚函数只能是类的成员函数, 而不能将类外的普通函数声明为虚函数. 虚函数的作用是允许在派生类中对基类的虚函数重新定义 (函数覆盖), 只能用于类的继承层次结构中.

虚函数能有效减少空间开销. 当一个类带有虚函数时, 编译系统会为该类构造一个虚函数表 (一个指针数组), 用于存放每个虚函数的入口地址.

什么时候应该使用虚函数:

判断成员函数所在的类是不是基类, 非基类无需使用虚函数 成员函数在类被继承后有没有可能被更改的功能, 如果希望修改成员函数功能, 一般在基类中将其声明为虚函数 我们会通过对象名还是基类指针访问成员函数, 如果通过基类指针过引用去访问, 则应当声明为虚函数

有时候在定义虚函数的时候, 我们无需定义其函数体. 它的作用只是定义了一个虚函数名, 具体的功能留给派生类去添加, 也就是纯虚函数. 例如我们在上面的 Animal 类的bark()函数就应该声明为纯虚函数, 因为 Animal 为基类, 定义bark()函数实体并无意义.

无论身处何处,只要有一颗放松而美好的心态,生活便是美好!

C/C++中虚函数详解及其作用介绍

相关文章:

你感兴趣的文章:

标签云: