一、实验目的和要求
了解静态联编和动态联编的概念。掌握动态联编的条件。
二、实验内容
1、分析并调试下列程序。
-
//sy6_1.cpp
-
-
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(float x){ cout<< "Derived::g(float)"<<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);
-
pb->f( 3.14f);
-
pb->g( 3.14f);
-
pb->h( 3.14f);
-
pb->h( 3.14f);
-
return 0;
-
}
(1)找出以上程序中使用了重载和覆盖的函数。
答:Base类中函数void g(); 和void h();与Derived类中的函数void g(); 和void h();函数名相同,参数类型不同,构成了函数重载。
(2)写出程序的输出结果,并解释输出结果。
2、分析并调试下列程序。
-
//sy6_2.cpp
-
-
using namespace std;
-
class Base
-
{
-
public:
-
void f(int x){ cout<< "Base::f(int)"<<x<< endl;}
-
void f(float x){ cout<< "Base::f(float)"<<x<< endl;}
-
virtual void g(void){ cout<< "Base::g(void)"<< endl;}
-
};
-
class Derived: public Base
-
{
-
public:
-