实验 六 多态性与虚函数

  • Post author:
  • Post category:其他



一、实验目的和要求


了解静态联编的动态联编的概念。掌握动态联编的条件。


二、实验内容

1.分析并调试下列程序。

//sy6_1.cpp
#include<iostream>
using namespace std;
class Base
{
    public:
        virtual void f(float x){cout<<"Base::f(float)"<<x<<endl;}
        void g(float x){cout<<"Base::g(float)"<<x<<endl;}
        void h(float x){cout<<"Base::h(float)"<<x<<endl;}
};
class Derived:public Base
{
   public:
        virtual void f(float x){cout<<"Derived::f(float}"<<x<<endl;}
        void g(int x){cout<<"Derived::g(int)"<<x<<endl;}
        void h(float x){cout<<"Derived::h(float)"<<x<<endl;}
};
int main()
{
    Derived d;
    Base *pb=&d;
    Derived *pd=&d;
    pb->f(3.14f);
    pd->f(3.14f);
    pb->g(3.14f);
    pb->h(3.14f);
    pd->h(3.14f);
  



版权声明:本文为zhangnian_666原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。