虚函数与多态

如果我们有三个类Person、Teacher、Student它们之间的关系例如以下:


这里写图片描写叙述
类的关系图

普通成员函数

【Demo1】
依据这个类图,我们有以下的代码实现

#ifndef __OBJEDT_H__
#define  __OBJEDT_H__

#include <string>
#include <iostream>

class Person
{
public:
    Person(const string& name, int age) : m_name(name), m_age(age)
    {
    }

    void ShowInfo()
    {
        cout << "姓名:" << m_name << endl;
        cout << "年龄:" << m_age << endl;
    }

protected:
    string  m_name;     //姓名
    int     m_age;      //年龄
};


class Teacher : public Person
{
public:
    Teacher(const string& name, int age, const string& title)
        : Person(name, age), m_title(title)
    {
    }

    void ShowInfo()
    {
        cout << "姓名:" << m_name << endl;
        cout << "年龄:" << m_age << endl;
        cout << "职称:" << m_title << endl;
    }

private:
    string  m_title;        //职称
};

class Student : public Person
{
public:
    Student(const string& name, int age, int studyId)
        : Person(name, age), m_studyId(studyId)
    {
    }

    void ShowInfo()
    {
        cout << "姓名:" << m_name << endl;
        cout << "年龄:" << m_age << endl;
        cout << "学号:" << m_studyId << endl;
    }

private:
    int m_studyId;          //学号
};

#endif  //__OBJEDT_H__

測试代码:

void test()
{
    Person* pPerson = new Person("张三", 22);
    Teacher* pTeacher = new Teacher("李四", 35, "副教授");
    Student* pStudent = new Student("王五", 18, 20151653);
    pPerson->ShowInfo();
    cout << endl;
    pTeacher->ShowInfo();
    cout << endl;
    pStudent->ShowInfo();
    cout << endl;
    delete pPerson;
    delete pTeacher;
    delete pStudent;
}

结果:

姓名:张三
年龄:22

姓名:李四
年龄:35
职称:副教授

姓名:王五
年龄:18
学号:20151653

说明:
这里的ShowInfo就是一个普通的函数。pPerson、pTeacher和pStudent三个对象调用ShowInfo分别展示自己的信息。
我们知道:父类的指针是能够指向子类的对象的。我们把以上測试代码略微改一下:
【Demo2】

void test()
{
    Person* pPerson = new Person("张三", 22);
    Person* pTeacher = new Teacher("李四", 35, "副教授");
    Person* pStudent = new Student("王五", 18, 20151653);
    pPerson->ShowInfo();
    cout << endl;
    pTeacher->ShowInfo();
    cout << endl;
    pStudent->ShowInfo();
    cout << endl;
    delete pPerson;
    delete pTeacher;
    delete pStudent;
}

结果:

姓名:张三
年龄:22

姓名:李四
年龄:35

姓名:王五
年龄:18

这时。pTeacher和pStudent仅仅输出了姓名和年龄,并没有输出子类所具有的特性(职称和学号)。

这应该不是你期望的结果,你可能期望pTeacher和pStudent输出老师和学生的完整信息,这时就须要用虚函数。

虚函数

我们把Person中的ShowInfo成员改成虚函数(在前面加上virtual),代码例如以下:
【Demo3】

class Person
{
public:
    Person(const string& name, int age) : m_name(name), m_age(age)
    {
    }

    virtual void ShowInfo()
    {
        cout << "姓名:" << m_name << endl;
        cout << "年龄:" << m_age << endl;
    }

protected:
    string  m_name;     //姓名
    int     m_age;      //年龄
};

在执行上面【Demo2】中的測试代码,得到我们想到的结果:

姓名:张三
年龄:22

姓名:李四
年龄:35
职称:副教授

姓名:王五
年龄:18
学号:20151653

虚函数使用方法要点:

  1. 虚函数的声明方式:

    virtual RETURN_TYPE functionName(ARGS 參数列表);

  2. 虚函数作用:现实C++中的多态。进行动态绑定(父类指针可指向子类的对象),直到执行时才知道要调用哪个版本号(哪个类定义)的函数;

  3. 我们必要对虚函数进行定义;
  4. 一旦父类的成员函数声明virtual,其子类的函数无论有没有声明为virtual,都是虚函数。
  5. 如果虚函数使用默认实參,父类和子类定义的默认实參最好一致。

【Demo4】:针对第4点说明:

class Person
{
public:
    Person(const string& name, int age) : m_name(name), m_age(age)
    {
    }

    virtual void ShowInfo()
    {
        cout << "姓名:" << m_name << endl;
        cout << "年龄:" << m_age << endl;
    }

    string GetName();       //正确,普通函数如果不被使用,能够仅仅有声明未定义
    virtual int GetAge();   //错误,虚函数必须要有定义。即使是一个空实现,由于编译器无法确定会使用哪个函数


protected:
    string  m_name;     //姓名
    int     m_age;      //年龄
};

【Demo5】:针对第5点进行说明:
设计我们的类例如以下定义。

class Person
{
public:
    virtual void SetAge(int age = 0)
    {
        m_age = age;
    }
    //... 省略
};


class Teacher : public Person
{
public:
    virtual void SetAge(int age = 1)
    {
        m_age = age;
    }

    //... 省略
};

class Student : public Person
{
public:
    virtual void SetAge(int age = 2)
    {
        m_age = age;
    }

    //... 省略
};

測试1:

void test()
{
    Person* pPerson = new Person("张三", 22);
    Teacher* pTeacher = new Teacher("李四", 35, "副教授");
    Student* pStudent = new Student("王五", 18, 20151653);
    pPerson->SetAge();
    pTeacher->SetAge();
    pStudent->SetAge();

    pPerson->ShowInfo();
    cout << endl;
    pTeacher->ShowInfo();
    cout << endl;
    pStudent->ShowInfo();
    cout << endl;
    delete pPerson;
    delete pTeacher;
    delete pStudent;
}

结果:

姓名:张三
年龄:0

姓名:李四
年龄:1
职称:副教授

姓名:王五
年龄:2
学号:20151653

測试2:

void test()
{
    Person* pPerson = new Person("张三", 22);
    Person* pTeacher = new Teacher("李四", 35, "副教授");
    Person* pStudent = new Student("王五", 18, 20151653);
    pPerson->SetAge();
    pTeacher->SetAge();
    pStudent->SetAge();

    pPerson->ShowInfo();
    cout << endl;
    pTeacher->ShowInfo();
    cout << endl;
    pStudent->ShowInfo();
    cout << endl;
    delete pPerson;
    delete pTeacher;
    delete pStudent;
}

结果:

姓名:张三
年龄:0

姓名:李四
年龄:0
职称:副教授

姓名:王五
年龄:0
学号:20151653

纯虚函数

在上面的样例中。我们如果全部的人都要工作,但不同的人工作的方式不同。于是我们就要强制要求继承自Person的子类都要有工作的方法,这就须要纯虚函数。定义例如以下:
【Demo6】

class Person
{
public:
    //... 省略
    virtual void DoWork() = 0;
        //... 省略
};

但此时我们编译

Person* pPerson = new Person("张三", 22);

这句话时会报错:error C2259: ‘Person’ : cannot instantiate abstract class
这是由于我们并没有为Person实现DoWork方法,而包括纯虚函数的类是一个抽象的类,抽象类不能被实例化。

于是我们在子类中对它实现例如以下:
【Demo7】

class Teacher : public Person
{
public:
    //... 省略
    virtual void DoWork()
    {
        cout << "教书..." << endl;
    }
    //... 省略
};

class Student : public Person
{
public:
    //... 省略
    virtual void DoWork()
    {
        cout << "学习..." << endl;
    }
    //... 省略
};

没用DoWork方法:

void test()
{
    Person* pTeacher = new Teacher("李四", 35, "副教授");
    Person* pStudent = new Student("王五", 18, 20151653);

    pTeacher->DoWork();
    cout << endl;
    pStudent->DoWork();
    cout << endl;

    delete pTeacher;
    delete pStudent;
}

结果:

教书…

学习…

纯虚函数使用方法要点:

  1. 纯虚函数的声明方式:

    virtual RETURN_TYPE functionName(ARGS 參数列表) = 0;

  2. 含有纯虚函数的类是一个抽象的类。抽象类不能被实例化。

  3. 包括纯虚函数的抽象类经常使用来当作对外的接口,说明这个类有什么功能,而没有详细的实现,基体的实现交由子类完毕。
原文地址:https://www.cnblogs.com/mengfanrong/p/5285155.html