虚函数与多态

假设我们有三个类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

虚函数用法要点:
虚函数的声明方式:

virtual RETURN_TYPE functionName(ARGS 参数列表);

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

我们必要对虚函数进行定义;
一旦父类的成员函数声明virtual,其子类的函数不管有没有声明为virtual,都是虚函数;
如果虚函数使用默认实参,父类和子类定义的默认实参最好一致。
【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;
}

结果:

教书…

学习…

纯虚函数用法要点:
纯虚函数的声明方式:

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

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

包含纯虚函数的抽象类常用来当作对外的接口,说明这个类有什么功能,而没有具体的实现,基体的实现交由子类完成。
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值