《C++语言教程》11章 虚函数和纯虚函数


一、类型转换又出了问题

学过Java的人反过来学C++,在继承上往往出很多问题。下面就是Java程序员容易出错的例子。


#include <iostream>
#include <string>
using namespace std;

class CBase {
public:
    void show() {
        cout << "BASE" << endl;
    }
};

class CDerive : public CBase {
public:
    void show() {
        cout << "DERIVE" << endl;
    }
};

int main ( )
{
    CDerive d;
    CBase& x = d;

    d.show();
    x.show();

    return 0;
}

变量x是变量d的别名,应该有相同的输出结果,问题出在哪儿呢?问题出在22行赋值的时候。赋值时进行了类型转换,将CDerive类型转换成了CBase类型。

二、虚函数

解决上面的问题很简单,基类中的show()函数改成虚函数,在派生类中用同名函数“实装”基类的虚函数。如果派生类中没有同名函数,则仍用基类的虚函数实装。


#include <iostream>
#include <string>
using namespace std;

class CBase {
public:
    virtual void show() {
        cout << "BASE SHOW" << endl;
    }
    virtual void print() {
        cout << "BASE PRINT" << endl;
    }
};

class CDerive : public CBase {
public:
    void show() {
        cout << "DERIVE SHOW" << endl;
    }
};

int main ( )
{
    CDerive d;
    CBase& x = d;

    d.show(),  x.show();    //虚函数只影响派生类的实装
    d.print(), x.print();

    CBase b;
    b.show(),  b.print();   //虚函数不影响基类自身的实例

    return 0;
}

注意:“虚继承”和“虚函数”是二个不同概念,“虚继承”针对基类的成员变量的二义性,而“虚函数”是针对成员函数的二义性。“虚继承”是将“virtual”加在派生类的定义处,“虚函数”是将“virtual”加在基类的成员函数的定义处。

三、纯虚函数

虚继承的应用使程序设计发生了很大变化,也就是在设计阶段只定义基类,而编程阶段通过继承来完成真正的功能。为了控制基类的成员函数在基类什么也不做,引进了“纯虚函数”的概念,使之没有定义体,因而也就不能产生实例。包含“纯虚函数”的类,因不能产生实例,所以称之为“抽象类”。

Java程序员注意:在Java中有类似“抽象类”的称之为“接口”的类,在Visual C++中也有,但这不是标准C++的内容。


#include <iostream>
#include <string>
using namespace std;

class CBase {
public:
    virtual void show() = 0;    //这是“纯虚函数”
    virtual void print() {      //这是普通的“虚函数”
        cout << "BASE PRINT" << endl;
    }
};

class CDerive : public CBase {
public:
    void show() {      //在派生类中,对“纯虚函数”必须实装
        cout << "DERIVE SHOW" << endl;
    }
};

int main ( )
{
    CDerive d;
    CBase& x = d;

    d.show(),  x.show();
    d.print(), x.print();

    //CBase b;     //抽象类不能实例化
    //b.show(),  b.show();

    return 0;
}