虚函数

虚函数在多态中间的使用:
多态一般就是通过指向基类的指针来实现的。

dog mydogwangwang;
mydogwangwang.born();
一定是返回“dog”

那么
horse myhorsepipi;
myhorsepipi.born();
一定是返回“horse”

也是多态呀?
/
有一点你必须明白,就是用父类的指针在运行时刻来调用子类:
例如,有个函数是这样的:
void animal::fun1(animal *maybedog_maybehorse)
{
maybedog_maybehorse->born();
}

参数maybedog_maybehorse在编译时刻并不知道传进来的是dog类还是horse类,
所以就把它设定为animal类,具体到运行时决定了才决定用那个函数。
也就是说用父类指针通过虚函数来决定运行时刻到底是谁而指向谁的函数

 


//用虚函数
#include <iostream.h>
class animal
{
    public:
        animal();
        ~animal();
        void fun1(animal *maybedog_maybehorse);
        virtual void born();
};
void animal::fun1(animal *maybedog_maybehorse)
{
    maybedog_maybehorse->born();
}
animal::animal()
{
}
animal::~animal()
{
}
void animal::born()
{
    cout<< "animal";
}

class dog: public animal
{
    public:
        dog();
        ~dog();
        virtual void born();
};
dog::dog()
{
}
dog::~dog()
{
}
void dog::born()
{
    cout<<"dog";
}

class horse:public animal
{
    public:
        horse();
        ~horse();
        virtual void born();
};

horse::horse()
{
}

horse::~horse()
{
}
void horse::born()
{
    cout<<"horse";
}
void main()
{
    animal a;
    dog b;
    horse c;
    a.fun1(&a);cout<<endl;
    a.fun1(&b);cout<<endl;
    //虚函数最关键的特点是“动态联编”,它可以在运行时判断指针指向的对象,并自动调用相应的函数。
    a.fun1(&c);cout<<endl;
    //虚函数最关键的特点是“动态联编”,它可以在运行时判断指针指向的对象,并自动调用相应的函数。
    b.fun1(&b);cout<<endl;
    b.fun1(&c);cout<<endl;
    //虚函数最关键的特点是“动态联编”,它可以在运行时判断指针指向的对象,并自动调用相应的函数。
    c.fun1(&c);cout<<endl;
    c.fun1(&a);cout<<endl;
    //虚函数最关键的特点是“动态联编”,它可以在运行时判断指针指向的对象,并自动调用相应的函数。
}
//output: horse


//不用虚函数
#include <iostream.h>
class animal
{
    public:
        animal();
        ~animal();
        void fun1(animal *maybedog_maybehorse);
        void born();
};
void animal::fun1(animal *maybedog_maybehorse)
{
    maybedog_maybehorse->born();
}
animal::animal()
{
}
animal::~animal()
{
}
void animal::born()
{
    cout<< "animal";
}

class dog: public animal
{
    public:
        dog();
        ~dog();
        void born();
};

dog::dog()
{
}
dog::~dog()
{
}
void dog::born()
{
    cout<<"dog";
}

class horse:public animal
{
    public:
        horse();
        ~horse();
        void born();
};
horse::horse()
{
}

horse::~horse()
{
}
void horse::born()
{
    cout<<"horse";
}
void main()
{
    animal a;
    dog b;
    horse c;
    a.fun1(&c);//它只能按照调用animal类的函数来理解并编译,所以我们看到了这样的结果。
    cout<<endl;
}
//output: animal

 


有纯虚函数的类是抽象类,不能生成对象,只能派生。他派生的类的纯虚函数没有被改写,
那么,它的派生类还是个抽象类。
---------------------------------------------------------------

定义纯虚函数就是为了让基类不可实例化化,
因为实例化这样的抽象数据结构本身并没有意义.
或者给出实现也没有意义
实际上我个人认为纯虚函数的引入,是出于两个目的,
1.为了安全.因为避免任何需要明确但是因为不小心而导致的未知的结果.
提醒子类去做应做的实现.
2.为了效率,不是程序执行的效率,而是为了编码的效率. 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值