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

时间:2022-04-22 21:57:22
#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.

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