C++中利用虚函数实现多态

本文通过两个示例深入解析C++中虚函数的工作原理,包括虚函数表的使用及如何实现多态。展示了基类指针指向派生类对象时调用虚函数的行为差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
using namespace std;
class A
{
    public:
        inline virtual void vfun()
        {
            cout << "this is vfun in class A" << endl;
        }
};
class B : public A
{
    public:
        inline void vfun()
        {
            cout << "this is vfun in class B" << endl;
        }
};
void test(void)
{
    A* p;
    A P;
    // 由于A中声明了虚函数,那么在定义对象P时,P中将包含A类的vfptr,该vfptr指向A类的虚函数表,
    p = &P;
    // 因此下面的语句向显示”this is vfun in class A”
    p->vfun();
    B* q;
    // 由于A中声明了虚函数,类B继承了A,那么在定义对象Q时,Q中将包含B类的vfptr,该vfptr指向B类的
    // 虚函数表
    B Q;
    q = &Q;
    p = q;
    // p 现在是类B对象的一个指针,即Q的地址,而Q中包含了指向B类虚函数表的vfptr指针。因此下面的语句将显示
    // “this is vfun in class B”
    p->vfun();

}
/**********************
该函数运行结果:
this is vfun in class A
this is vfun in class B
************************/

class Parent
{
    public:
        int parent;
        inline Parent(){}
        inline Parent(int parent) { this->parent = parent; }
        inline int return_parent(){return parent;}
        inline virtual int Return() { return parent; }
        inline virtual void haveFun() { cout << "haveFun in Parent" << endl; }
};
class Child : public Parent
{
    public:
    int child;
        inline Child(){}
        inline Child(int child) : Parent(child-25) { this->child = child; }
        inline int Return() { return child ; }
        inline void haveFun() { cout << "haveFun in Child" << endl; }
};
int main(void)
{
    Parent* p = new Child(1991);
    p->haveFun();
    cout<<"p->return_parent() is :"<<p->return_parent()<<'\n';
    //假如Parent 类Return()不声明为虚函数,则下边语句输出1966
    //Parent 类Return()声明为虚函数,则下边语句输出1991
    cout<<"p->Return() is :"<<p->Return()<<'\n';
    return 0;
}
/**************************
运行结果:
haveFun in Child
p->return_parent() is :1966
p->Return() is :1991

Process returned 0 (0x0)   execution time : 0.016 s
Press any key to continue.

***************************/

#include <iostream>
using namespace std;
class A
{
    public:
        inline virtual void vfun()
        {
            cout << "this is vfun in class A" << endl;
        }
};
class B : public A
{
    public:
        inline void vfun()
        {
            cout << "this is vfun in class B" << endl;
        }
};
void test(void)
{
    A* p;
    A P;
    // 由于A中声明了虚函数,那么在定义对象P时,P中将包含A类的vfptr,该vfptr指向A类的虚函数表,
    p = &P;
    // 因此下面的语句向显示”this is vfun in class A”
    p->vfun();
    B* q;
    // 由于A中声明了虚函数,类B继承了A,那么在定义对象Q时,Q中将包含B类的vfptr,该vfptr指向B类的
    // 虚函数表
    B Q;
    q = &Q;
    p = q;
    // p 现在是类B对象的一个指针,即Q的地址,而Q中包含了指向B类虚函数表的vfptr指针。因此下面的语句将显示
    // “this is vfun in class B”
    p->vfun();

}
/**********************
该函数运行结果:
this is vfun in class A
this is vfun in class B
************************/

class Parent
{
    public:
        int parent;
        inline Parent(){}
        inline Parent(int parent) { this->parent = parent; }
        inline int return_parent(){return parent;}
        inline virtual int Return() { return parent; }
        inline virtual void haveFun() { cout << "haveFun in Parent" << endl; }
};
class Child : public Parent
{
    public:
    int child;
        inline Child(){}
        inline Child(int child) : Parent(child-25) { this->child = child; }
        inline int Return() { return child ; }
        inline void haveFun() { cout << "haveFun in Child" << endl; }
};
int main(void)
{
    Parent* p = new Child(1991);
    p->haveFun();
    cout<<"p->return_parent() is :"<<p->return_parent()<<'\n';
    //假如Parent 类Return()不声明为虚函数,则下边语句输出1966
    //Parent 类Return()声明为虚函数,则下边语句输出1991
    cout<<"p->Return() is :"<<p->Return()<<'\n';
    return 0;
}
/**************************
运行结果:
haveFun in Child
p->return_parent() is :1966
p->Return() is :1991

Process returned 0 (0x0)   execution time : 0.016 s
Press any key to continue.

***************************/




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值